NSAttributedString是用于富文本编辑的,跟NSString和NSDictionary一样有一个可变的NSMutableAttributedString,一帮我们在使用的时候都是用NSMutableAttributedString。
NSMutableAttributedString创建和使用:
方式一:
NSString *astr = @"this is a string"; //创建字符串
NSMutableAttributedString *attributestring = [[NSMutableAttributedString alloc]initWithString:astr]; //创建富文本
[attributestring addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, astr.length)]; //配置富文本
label.attributedText = attributestring; //赋值
方式二:
NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30]}; //配置富文本
label.attributedText = [[NSAttributedString alloc] initWithString: astr attributes: attrDict2]; //创建并赋值
NSAttributedString 不同形式的属性
1.NSFontAttributeName 字体大小 (默认Helvetica 12)
2.NSParagraphStyleAttributeName 段落分部
3.NSForegroundColorAttributeName 字体颜色 (默认是黑色)
4.NSBackgroundColorAttributeName 背景颜色 (默认是黑色)
5.NSLigatureAttributeName 连体字符 (默认是0,0表示不连体,1表示连体)
6.NSKernAttributeName 字符之间的距离
7.NSUnderlineStyleAttributeName 下划线 (有单下划线双下划线,和粗细之分有中英文之分)
8.NSStrikethroughStyleAttributeName 删除线 一共有四种情况(和删除线类是)
9.NSStrokeColorAttributeName 填充字体的颜色
10.NSStrokeWidthAttributeName 字体是否有填充 取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
11.NSShadowAttributeName 阴影效果 取值为 NSShadow 对象
12.NSTextEffectAttributeName 印刷效果
13.NSLinkAttributeName 连接效果 (加入响应的url,点击之后跳转到响应的网页)
14.NSBaselineOffsetAttributeName 基准线 取值为 NSNumber,正值上偏,负值下偏
15.NSUnderlineColorAttributeName 下划线颜色 取值为UIColor
16.NSStrikethroughColorAttributeName 删除线颜色 取值为UIColor
17.NSObliquenessAttributeName 字体倾斜度 正值右倾,负值左倾
18.NSExpansionAttributeName 文本拉伸和压缩(正值拉升,负值压缩)
19.NSWritingDirectionAttributeName 文字的书写方向 从左到右,从右到左
20.NSAttributedString 图文编排效果
21.NSVerticalGlyphFormAttributeName 设置文字排版防线 0 表示横排文本,1 表示竖排文本
1~11和21编号的属性是iOS6加入的 12~20编号的属性是iOS7加入的