初始化
常用
1 | - (instancetype)initWithString:(NSString *)str; |
非常用
1 | - (nullable instancetype)initWithURL:(NSURL *)url options:(NSDictionary<NSAttributedStringDocumentReadingOptionKey, id> *)options documentAttributes:(NSDictionary<NSAttributedStringDocumentAttributeKey, id> * __nullable * __nullable)dict error:(NSError **)error NS_AVAILABLE(10_4, 9_0); |
例如:
1 | NSData * data = [@"<html>text<\html>" dataUsingEncoding:NSUnicodeStringEncoding]; |
其中 NSDocumentTypeDocumentAttribute 这个属性有如下的几种取值:
- NSPlainTextDocumentType (普通的文本)
- NSRTFTextDocumentType (富文本)
- NSRTFDTextDocumentType (带有附件的富文本)
- NSHTMLTextDocumentType (HTML格式的文本)
常用属性
属性 | 名 | 值类型 | 默认值 |
---|---|---|---|
NSFontAttributeName | 字体 | UIFont | Helvetica(Neue) 12 |
NSParagraphStyleAttributeName | 段落样式 | NSParagraphStyle | defaultParagraphStyle |
NSForegroundColorAttributeName | 前景色 | UIColor | blackColor |
NSBackgroundColorAttributeName | (文本)背景色 | UIColor | nil: no background |
NSLigatureAttributeName | 连笔 | NSNumber | 这个属性的取值为整数。默认值为1,代表使用默认的连体字符; 取值0,表示没有连体字符。 |
NSKernAttributeName | 间距 | NSNumber | 字符间距,取正值时间距增加,取负值时间距减小,0时间距无效 |
NSStrikethroughStyleAttributeName | 删除线 | NSNumber | 取整数值默认为0,无删除线 |
NSUnderlineStyleAttributeName | 下划线 | NSNumber | 取整数值默认为0,无下划线 |
NSStrokeColorAttributeName | 填充颜色 | UIColor | 默认为 nil: 此时颜色同 foreground color |
NSStrokeWidthAttributeName | 填充(描边,加粗) | NSNumber | 正值为中空的效果,负值为填充的效果,默认值为0 |
NSShadowAttributeName | 阴影 | NSShadow | 默认是nil,没有阴影效果 |
NSTextEffectAttributeName | 文本特殊效果 | NSString | 默认是nil,没有特殊的文字效果 |
NSAttachmentAttributeName | 文本附件,插入图片 | NSTextAttachment | 默认 nil |
NSLinkAttributeName | 链接 | NSURL or NSString | NSURL (preferred) or NSString |
NSBaselineOffsetAttributeName | 基准线偏移 | NSNumber | 默认0,垂直方向上,整数往上,负数往下 |
NSUnderlineColorAttributeName | 下划线颜色 | UIColor | 默认 nil: 同 foreground color |
NSStrikethroughColorAttributeName | 删除线颜色 | UIColor | 默认 nil: 同 foreground color |
NSObliquenessAttributeName | 倾斜 | NSNumber | 默认0,正向右倾斜,负向左倾斜 |
NSExpansionAttributeName | 扁平化 | NSNumber | 默认0,正拉伸,负压缩 |
NSWritingDirectionAttributeName | 文字方向 | NSArray of NSNumbers | |
NSVerticalGlyphFormAttributeName | 水平或竖直文本 | NSNumber |