NSAttributedString叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体、字号、字体大小等各不相同的风格,还可以对段落进行格式化。

通过以下代码即可实现上面图示效果,十分方便,从此再也不用设置两个UILable,并且处心积虑的处理它们的长度了。

 1     UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 200, 40)];
2 aLable.textAlignment = NSTextAlignmentCenter;
3 [self.view addSubview:aLable];
4
5 NSString * aString = @"¥150 元/位";
6
7 //富文本对象
8 NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString];
9
10 //富文本样式
11 [aAttributedString addAttribute:NSForegroundColorAttributeName //文字颜色
12 value:[UIColor redColor]
13 range:NSMakeRange(0, 4)];
14
15 [aAttributedString addAttribute:NSFontAttributeName //文字字体
16 value:[UIFont systemFontOfSize:25]
17 range:NSMakeRange(0, 4)];
18
19 aLable.attributedText = aAttributedString;

常用属性:

NSFontAttributeName           文字字体

NSParagraphStyleAttributeName     段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)

 [段落样式-插曲]
1 @property(readwrite) CGFloat lineSpacing;              //行间距
2 @property(readwrite) CGFloat paragraphSpacing;           //段间距
3 @property(readwrite) NSTextAlignment alignment;           //对齐方式
4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧
5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进
6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度
7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式
8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高
9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高
10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
 [段落样式demo]
1 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 200)];
2 lable.backgroundColor = [UIColor lightGrayColor];
3 lable.numberOfLines = 0;
4 [self.view addSubview:lable];
5
6 NSString * string = @"Always believe that something wonderful is about \nto happen!";
7
8 //富文本
9 NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];
10
11 //段落样式
12 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
13
14 #warning lable.numberOfLines必须为0,段落样式才生效
15 //行间距
16 paragraphStyle.lineSpacing = 10.0;
17 //段落间距
18 paragraphStyle.paragraphSpacing = 20.0;
19
20 // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
21 // paragraphStyle.firstLineHeadIndent = 10.0;
22 // paragraphStyle.headIndent = 50.0;
23 // paragraphStyle.tailIndent = 200.0;
24
25 [attributedString addAttribute:NSParagraphStyleAttributeName
26 value:paragraphStyle
27 range:NSMakeRange(0, string.length)];
28
29 lable.attributedText = attributedString;


NSForegroundColorAttributeName    文字前景色

NSBackgroundColorAttributeName     文字背景色

NSLigatureAttributeName        连体字(NSNumber  @0:无连体,@1:默认连体,系统字体不包含对连体的支持)

NSUnderlineStyleAttributeName     下划线

NSStrokeColorAttributeName       只有在NSStrokeWidthAttributeName设置了值之后才有效(默认字体颜色和前景色一致,如果设置的颜色和前景色不一致则前景色无效)

NSStrokeWidthAttributeName      设置该属性之后字体变成空心字体,字体边线宽度为value设定的值

NSBaselineOffsetAttributeName     值为NSNumber类型,表明文字相对于其他文字基准线向上的偏移量

NSUnderlineColorAttributeName      值为UIColor类型,下划线颜色(只有在NSUnderlineStyleAttributeName的value为@1时有效)

NSUnderlineStyleAttributeName      值为NSNumber类型,下划线宽度(默认值为@0:下划线宽度为0——不现实下划线,@1:字符串有下划线)

属性挺多的,有其他需要的话command点进去看一下就ok,如果对他们的功能不了解三根指头点一下关键词,或者按住option点一下看看官方文档的Description相信就会有所了解了,其他的暂时就不介绍了。

如有问题,欢迎指正,小弟在此拜谢。

这里有篇讲解富文本的文章:http://www.2cto.com/kf/201409/334308.html

iOS之富文本(一)的更多相关文章

  1. iOS - NSMutableAttributedString富文本的实现

    NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...

  2. iOS之富文本

    之前做项目时遇到一个问题: 使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结果在XCode中查 ...

  3. iOS之富文本(二)

    之前做项目时遇到一个问题:          使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ...

  4. iOS swift 富文本显示 富文本在iOS中使用场景和解决方案

    项目中很多地方都会用到富文本的内容:比如一般的商品详情,视频详情,资讯详情等,运营人员通过后台的富文本编辑器编辑的内容,前端拿到的就是一段富文本的字符串,这富文本大多都是图片和文字的组合.我们今天介绍 ...

  5. IOS Html富文本渲染方式:DTCoreText、WKWebView、UIWebView的内存占用对比

    在app的内容页(详情页)中,富文本的显示一直是经常需要处理的问题,而通常在后端的富文本编辑中,Html应用比较普遍,所以其实需要处理的Html富文本显示的问题,以下这三种方式肯定不是最优的显示Htm ...

  6. iOS开发富文本制作 图片和文字/NSMutableParagraphStyle/NSMutableAttributedString

    /NSMutableParagraphStyle/NSMutableAttributedString 组合使 NSString * titlestr=@"日产GT-R"; NSMu ...

  7. iOS计算富文本(NSMutableAttributedString)高度

    有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ...

  8. iOS -- YYText富文本

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: [NSString strin ...

  9. iOS 开发富文本之TTTAttributedLabel 在某个特定位置的文字添加跳转,下划线,修改字体大小,颜色

    @property(nonatomic , strong) TTTAttributedLabel * ttLabel; @property(nonatomic , strong) NSRange li ...

随机推荐

  1. js判断PC端 移动端 并跳转到对应页面

    一.PC端跳转到移动端 html页面: <script>var webroot="/",catid="{$catid}",murl="m/ ...

  2. Ubuntu16.04使用Tarball安装ntp

    最近在学习linux,看书上例子(鸟哥的linux私房菜 P674),使用Tarball来安装ntp,出了点问题,提示错误,使用 ./configure 来检测程序时,出现如下提示: 提示少了 ope ...

  3. Ubuntu—安装python的第三方包gevent

    今晚花很多时间, 使用 sudo pip3 install gevent 但是始终没有成功. 柳暗花明又一村 sudo apt-get install python3-gevent 搞定!!! 人生如 ...

  4. 数据库与数据仓库的比较Hbase——Hive

    数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented).集成的(Integrate).相对稳定的(Non-Volatile).反映历史变化(Time Varian ...

  5. django的htpp请求之WSGIRequest

    WSGIRequest对象 Django在接收到http请求之后,会根据http请求携带的参数以及报文信息创建一个WSGIRequest对象,并且作为视图函数第一个参数传给视图函数.这个参数就是dja ...

  6. centos下安装升级python到python3.5

    本文摘抄自:https://www.cnblogs.com/edward2013/p/5289056.html  请支持原版 CentOS7安装Python3.5   2. 安装Python的依赖包 ...

  7. C语言问卷调查表

    你对自己的未来有什么规划?做了哪些准备?    对未来比较迷茫,现在的主要任务是学好专业课 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?   活到老学到老,学习是一辈子的事.在学习的过程 ...

  8. [图算法] 1003. Emergency (25)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  9. iOS- Swift:如何使用iOS8中的UIAlertController

    1.前言 在前段时间手机QQ:升级iOS8.3后,发图就崩的情况, 就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的 具体原因分析 这个可以看腾讯团队发出 ...

  10. Sql server 中关闭ID自增字段(SQL取消ID自动增长)

    sql server在导入数据的时候,有时候要考虑id不变,就要先取消自动增长再导入数据,导完后恢复自增. 比如网站改版从旧数据库导入新数据库,数据库结构不相同,可能会使用insert into xx ...