iOS中富文本的AttributeNames

本文主要为项目中无处不在的NSAttributedString的属性做一次整理,方便之后开发时查询和测试

目录:

1. NSFontAttributeName;
2. NSParagraphStyleAttributeName;
3. NSForegroundColorAttributeName;
4. NSBackgroundColorAttributeName;
5. NSLigatureAttributeName;
6. NSKernAttributeName;
7. NSStrikethroughStyleAttributeName;
8. NSUnderlineStyleAttributeName;
9. NSStrokeColorAttributeName;
10.NSStrokeWidthAttributeName;
11.NSShadowAttributeName;
12.NSTextEffectAttributeName;
13.NSAttachmentAttributeName;
14.NSLinkAttributeName;
15.NSBaselineOffsetAttributeName;
16.NSUnderlineColorAttributeName;
17.NSStrikethroughColorAttributeName;
18.NSObliquenessAttributeName;
19.NSExpansionAttributeName;
20.NSWritingDirectionAttributeName;
21.NSVerticalGlyphFormAttributeName;


1.NSFontAttributeName//字体 UIFont

2.NSParagraphStyleAttributeName//段落样式

以下截图来自YYText

3.NSForegroundColorAttributeName //字颜色 UIColor

4.NSBackgroundColorAttributeName //背景色 UIColor


5.NSLigatureAttributeName //连写,iOS只支持@(0)和@(1)


6.NSKernAttributeName//字间距


7.NSStrikethroughStyleAttributeName // 删除线

1
2
3
4
5
@(NSUnderlineStyle)枚举值
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02, 2~8 取值越大,线越粗
NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09

剩下的几个枚举需要配合上面的枚举来使用

1
2
3
4
5
6
7
NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0)      = 0x0000,//实线
NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,//短 循环
NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,//长 循环
NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,//长短 循环
NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,//长短短 循环

NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000//按单词分割

8.NSUnderlineStyleAttributeName// 下划线(值也是枚举NSUnderlineStyle的数字类型-@(NSUnderlineStyle)参考NSStrikethroughStyleAttributeName)


9.NSStrokeColorAttributeName// 笔画宽度和当前字的pointSize(字体大小)的比例,

正数真空效果


10.NSStrokeColorAttributeName//NSStrokeColorAttributeName的颜色


11.NSShadowAttributeName //阴影,参考NSShadow


12.NSTextEffectAttributeName//凸版印刷体(现在就只有NSTextEffectLetterpressStyle一个值)

凸版印刷替效果是给文字加上奇妙阴影和高光,让文字看起有凹凸感,像是被压在屏幕上(这个描述真是有够夸张 = =!)


13.NSAttachmentAttributeName//图文混排相关

1
2
3
NSTextAttachment *attach=[[NSTextAttachment alloc]init];
attach.image=[UIImage imageNamed:@"1178298162bf1917"];
[base insertAttributedString:[NSAttributedString attributedStringWithAttachment:attach] atIndex:6];


14.NSLinkAttributeName//链接,但是不负责点击的处理

1
2
3
4
NSMutableAttributedString *base=[[NSMutableAttributedString alloc]initWithString:string attributes:nil];
NSRange rang=NSMakeRange(0, base.length);
[base addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.google.com"]
range:[string rangeOfString:@"http://www.google.com"]];

一般配合UITextView使用

1
2
3
4
5
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(@"=============%@",URL);
return YES;
}


15.NSBaselineOffsetAttributeName//离BaseLine的距离

什么是BaseLine?


16.NSUnderlineColorAttributeName//下划线的颜色

相关属性(NSUnderlineStyleAttributeName)

17.NSStrikethroughColorAttributeName//中划线的颜色

相关属性(NSStrikethroughStyleAttributeName)


18.NSObliquenessAttributeName//倾斜


19.NSExpansionAttributeName//“胖”or “瘦”(拉伸or压缩)


20.NSWritingDirectionAttributeName//文字的排布顺序(从左到右还是从右到左)

1
2
3
4
5
//不太明白这个枚举值的两个意思...之后如果有机会明白再解释他们
typedef NS_ENUM(NSInteger, NSWritingDirectionFormatType) {
NSWritingDirectionEmbedding = (0 << 1),
NSWritingDirectionOverride = (1 << 1)
} NS_ENUM_AVAILABLE(10_11, 9_0);

21.NSVerticalGlyphFormAttributeName//0横,1竖

在iOS上只有横排,不过可以通过CoreText来修改成竖排