设为首页 加入收藏

TOP

iOS使用NSMutableAttributedString实现富文本小结(一)
2019-08-26 07:01:16 】 浏览:60
Tags:iOS 使用 NSMutableAttributedString 实现 文本 小结

NSAttributedString

NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距)。NSAttributedString对象的默认字体是Helvetica 12点,可能与平台的默认系统字体不同。因此,您可能希望创建适用于您的应用程序的非默认属性的新字符串。您还可以使用NSParagraphStyle类及其子类NSMutableParagraphStyle来封装NSAttributedString类使用的段落或标尺属性。

实例化方法和使用方法

实例化方法

使用字符串初始化

- (instancetype)initWithString:(NSString *)str; 

代码示例

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据"]; 

字典中存放一些属性名和属性值

- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs; 

代码示例

NSDictionary *attributedDict = @{ NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据" attributes:attributedDict]; 

使用NSAttributedString初始化,与NSMutableString,NSString类似

- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr; 

使用方法

为某一范围内的文字设置多个属性的方法

- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range; 

//代码示例

NSString *string = @"测试数据"; NSDictionary *attributedDict = @{ NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)]; 

为某一范围内的文字添加某个属性的方法

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; 

//代码示例

NSString *string = @"测试数据"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)]; 

为某一范围内的文字添加多个属性的方法

- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range; 

//代码示例

NSString *string = @"测试数据"; NSDictionary *attributedDict = @{ NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)]; 

移除某个范围内的某个属性的方法

- (void)removeAttribute:(NSString *)name range:(NSRange)range; 

//代码示例

 NSString *string = @"测试数据"; NSDictionary *attributedDict = @{ NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)]; [attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)]; 

属性及说明

key 说明
NSFontAttributeName 字体,val
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ios 开发UI篇—UITextView 下一篇苹果电脑(Mac mini或Macbook或iM..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目