1,NSAttributedString属性汇总
2,段落属性汇总
NSParagraphStyleAttributeName:值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。NSMutableParagraphStyle与NSParagraphStyle包括一下属性
3,设置富文本
获取富文本的四种方式
方式1:
//初始化富文本对象:
NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"1,我们都有一个家名字叫中国"];
//给富文本添加属性1-字体大小
[attributedStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16.0]
range:NSMakeRange(2, 2)];
//给富文本添加属性1-字间距
[attributedStr addAttribute:NSKernAttributeName
value:@10
range:NSMakeRange(2, 2)];
方式2:
//初始化富文本对象
NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@"2,我们都有一个家名字叫中国"];
//富文本的属性通过字典的形式传入
NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:16.0],NSFontAttributeName,//字体
[UIColor redColor],NSForegroundColorAttributeName,//字体颜色
[UIColor greenColor],NSBackgroundColorAttributeName,//字体背景色
@(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName,//下划线
@(NSUnderlineStyleSingle),NSStrikethroughStyleAttributeName,//删除线
[UIColor blueColor],NSUnderlineColorAttributeName,//下划线颜色
[UIColor yellowColor],NSStrikethroughColorAttributeName,//删除线颜色
[NSURL URLWithString:@"http://www.baidu.com"],NSLinkAttributeName,
nil];
//统一设置富文本对象的属性
[attributedStr addAttributes:attributeDict range:NSMakeRange(2, 2)];
//或者也可以使用下面的方法
[attributedStr setAttributes:attributeDict range:NSMakeRange(4, 2)];
方法3:
//富文本的属性通过字典的形式传入
NSDictionary *attributeDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:16.0],NSFontAttributeName,//字体大小
[UIColor redColor],NSForegroundColorAttributeName,//字体颜色
@(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName,//下划线
nil];
//初始化富文本对象的同时设置富文本对象的属性
NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:@"3,我们都有一个家名字叫中国" attributes:attributeDict];
方法4:
NSString * htmlString = @"<html><body> <font size=\"8\" color=\"blue\">4,我们都有一个家名字叫</a> <a></font> <font size=\"13\" color=\"red\">中国!</font></a> </body></html>";
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
4,获取富文本
//获取label的富文本属性attributedText
NSRange rang3 = NSMakeRange(0, label3.text.length);
[label3.attributedText enumerateAttribute:NSParagraphStyleAttributeName inRange:rang3 options:NSAttributedStringEnumerationReverse usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
NSMutableParagraphStyle *style = (NSMutableParagraphStyle *)value;
NSLog(@"lineSpace是:%f",style.lineSpacing);
}];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)