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. Shiro 修改权限,刷新权限

    shiro 访问鉴权:Realm AuthorizingRealm->doGetAuthorizationInfo doGetAuthorizationInfo protected abstra ...

  2. Zabbix自动发现之fping

    原文发表于cu:2016-06-21 Zabbix自动发现功能从配置流程上比较简单:Discovery与Action. 在做Zabbix的自动发现验证时,使用"ICMP ping" ...

  3. mongodb4简明笔记

    就一数据库,掌握基本用法,其他的现学现卖就行了. 所以要把握基本套路. 创建数据库=>使用数据库=>创建集合=>使用集合=>创建文档=>使用文档 1.数据库 mongod ...

  4. nordic-mesh中应用的代码实现

    nordic-mesh中应用的代码实现 Nordic-Mesh遵循SIG-Mesh-Profile中的mesh定义,实现了element.model等概念. 一个应用中包含一个或多个element,e ...

  5. BFC的表象认识

    首先字面翻译,这三个字母分别代表什么,box,formatting, context,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用. 形象点就是说一种规范,规范什么呢?规范盒子内部 ...

  6. $_SERVER的详细参数整理下

    PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行 脚本的文件名,与 document roo ...

  7. Thunder--Beta发布--美工+文案

    作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1366 内容: 美工:原有功能展示.新增功能展示 程序图标 欢迎页面 我的书架 ...

  8. win10自带中文输入法的用户体验

    用户界面: 貌似没有什么界面,不过我感觉这就是最大的优点,没有过度渲染的界面,没有烦人的推送.弹窗,没有定期不定期的更新提示,简洁也是我使用这款输入法的最主要的原因 记住用户的选择: 这点我认为win ...

  9. struts2--文件上传类型3

    拦截器栈在<package>标签内 <action>标签外配置 如上我们如果把它定义成默认拦截器的话就不需要在 <action>标签中引入,没有的话需要引入拦截器 ...

  10. 团队作业4——第一次项目冲刺(Alpha版本)第一次

    一.会议内容 制定任务内容 制作leangoo表格 初步工作 二.各人工作 成员 计划任务 遇见难题 贡献比 塗家瑜(组长) 后端与数据库通讯 无 1 张新磊 表设计 无 1 姚燕彬 测试计划编写 无 ...