11.同一个Label中显示不同字体(NSAttributedString)
2016-02-19 10:57
405 查看
在开发中经常碰到一句话, 显示的字体大小或者颜色不同的情况,如下图情况。 当然可以用多个 UILabel 去显示, 但是性能和繁杂程度的角度来说 , 在同一个 Label 中使用NSMutableAttributedString 类会更加方便。而且还有很多更多的功能,如添加下划线,双下划线等等, 接下来,我们一起来看看。
首先, 你要准备几种字体,你可以参考 9. iOS 开发中添加自定义汉语字体 文章中如何获取所有的字体类型的方法, 也可以参考 8.iOS 字体类型列表 文章中的字体类型。
UILabel 有个属性attributedText, 该属性默认为 nil, 当设置了该属性后,其他关于 label.text 的属性就会失效。
// the underlying attributed string drawn by the label, if set, the label ignores the properties above.
@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
NSMutableAttributedString类中常用的三个方法:
1.addAttribute: value: range: 方法使用, 可以对某个给定的 range 进行字体颜色或者大小或者风格的特质属性添加。
以上代码的结果是上图展示的内容。
2.addAttributes: range: 方法使用, 只可以对某个给定的 range 进行字体颜色和大小和风格的特质属性添加。注意此处用词为”和”不是”或”,第一种方法在一行代码下只可以做出某一种改变, 本方法可以做出多种改变。(第一种方法多次添加几种改变,效果可达到一致)
结果展示
3.removeAttribute: range: 方法, 移除某个给定范围的特质属性。
延伸资料:iOS NSAttributedString所有文本属性详解(多图)
首先, 你要准备几种字体,你可以参考 9. iOS 开发中添加自定义汉语字体 文章中如何获取所有的字体类型的方法, 也可以参考 8.iOS 字体类型列表 文章中的字体类型。
UILabel 有个属性attributedText, 该属性默认为 nil, 当设置了该属性后,其他关于 label.text 的属性就会失效。
// the underlying attributed string drawn by the label, if set, the label ignores the properties above.
@property(nullable, nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
NSMutableAttributedString类中常用的三个方法:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; - (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range; - (void)removeAttribute:(NSString *)name range:(NSRange)range; //
1.addAttribute: value: range: 方法使用, 可以对某个给定的 range 进行字体颜色或者大小或者风格的特质属性添加。
NSMutableAttributedString *newAttrStr = [[NSMutableAttributedString alloc] initWithString:@"1071.22元"]; [newAttrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:40] range:NSMakeRange(0,length)]; [newAttrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:NSMakeRange(length,1)]; accountMoneyLabel.attributedText = newAttrStr; //
以上代码的结果是上图展示的内容。
2.addAttributes: range: 方法使用, 只可以对某个给定的 range 进行字体颜色和大小和风格的特质属性添加。注意此处用词为”和”不是”或”,第一种方法在一行代码下只可以做出某一种改变, 本方法可以做出多种改变。(第一种方法多次添加几种改变,效果可达到一致)
NSMutableAttributedString *currentPriceStr = [[NSMutableAttributedString alloc] initWithString: @"最低¥8.88元"]; [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia-Bold" size:13],NSForegroundColorAttributeName:[UIColor brownColor],NSUnderlineStyleAttributeName:[NSNumber numberWithInt:NSUnderlineStyleDouble]} range:NSMakeRange(0, 2)]; [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Verdana-Italic" size:22],NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(2, 5)]; Label.attributedText = currentPriceStr; //
结果展示
3.removeAttribute: range: 方法, 移除某个给定范围的特质属性。
Attributes
在NSAttributedString.h文件中, 有各种可添加的Attributes和Attribute values, 常用的有字体名称/前景色/背景色/下划线风格等等, 具体的可以慢慢探究。延伸资料:iOS NSAttributedString所有文本属性详解(多图)
相关文章推荐
- Zabbix 3.0 发布:加强了云端运行监视的安全性
- MAC机中安装RUBY环境
- 滑动隐藏和显示标题栏
- Eclipse attach source code
- hrtimer内核配置
- android ListView和GridView拖拽移位实现代码
- Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec
- 卖书记
- Odoo8.0中允许用户动态调整TreeView栏位宽度
- 检查Android是否具有摄像头
- 关于大型网站技术演进的思考(八)--存储的瓶颈终篇(8)
- SQL数据批量提交的优化和心得
- Android 启动模式简析
- oracle distinct多字段去重
- 二、后台定位
- 全局变量、常量、共享数据、跨webview传参的综述
- 多媒体数据压缩
- 《Node.js入门》CentOS 6.5下Node.js Web开发环境搭建笔记
- paper 22:kl-divergence(KL散度)实现代码
- android客户端与javaweb服务端通信——登录小demo的启示