iOS里面的富文本
- 1、NSAttributedString属性概览表
- 2、属性详解及应用
-
- 2.1 NSAttributedString.Key.font --字体大小
- 2.2 NSAttributedString.Key.paragraphStyle -- 文本字、行间距,对齐等
- 2.3 NSAttributedString.Key.foregroundColor -- 字体颜色
- 2.4 NSAttributedString.Key.backgroundColor -- 背景色
- 2.5 NSAttributedString.Key.ligature -- 连字符
- 2.6 NSAttributedString.Key.kern -- 字符间距
- 2.7 设置删除线相关
- 2.8 设置下划线相关
- 2.9 描边颜色、描边宽度
- 2.10 文本阴影
- 2.11 文字效果
- 2.12 链接
- 2.13 基础偏移量
- 2.14 字体倾斜
- 2.15 文本扁平化(横向拉伸)
- 3、富文本加载html
- 4、图文混排
- 5、巧用UITextView实现富文本的点击效果
- 参考文章
ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线、删除线、斜体、空心字体、背景色、阴影以及图文混排(一种文字中夹杂图片的显示效果)。通常想要实现这些效果要使用到iOS的Fundation框架提供的NSAttributeString类。
1、NSAttributedString属性概览表
Key | Value | 说明 |
---|---|---|
.font | UIFont对象 | 字体大小:默认Helvetica(Neue) 12 |
.paragraphStyle | NSParagraphStyle对象 | 文本字、行间距,对齐等:默认NSParagraphStyle.default |
foregroundColor | UIColor对象 | 字体颜色:默认.black |
.backgroundColor | UIColor对象 | 背景色:默认nil(无背景色) |
.ligature | 包含整数的NSNumber对象 | 连字符:ios中有0和1两个值;0表示没有连字符,而1是默认的连字符 |
.kern | 包含浮点数的NSNumber对象 | 字符间距:默认0(禁用) |
.strikethroughStyle | 包含整数的NSNumber对象 | 删除线:默认0(无删除线) |
.underlineStyle | 包含整数的NSNumber对象 | 下划线:默认0(无下划线) |
.strikethroughColor | UIColor对象 | 删除线颜色:默认 nil(和文字的 foregroundColor一致) |
.underlineColor | UIColor对象 | 下划线颜色:默认nil(和文字的 foregroundColor一致) |
.strokeColor | UIColor对象 | 描边颜色:nil(和文字的 foregroundColor一致) |
strokeWidth | 包含浮点数的NSNumber对象 | 描边宽度:正值空心描边,负值实心描边,默认0(不描边) |
.shadow | NSShadow对象 | 文本阴影:默认nil(没有阴影) |
.textEffect | NSString对象 | 文字效果:默认nil(没有文字效果) |
.attachment | NSTextAttachment对象 | 附件(常用作图文混排) :默认nil(没有附件) |
.link(原名:NSLinkAttributeName) | NSURL (优先) 或 NSString对象 | 链接 |
.baselineOffset | 包含浮点数的NSNumber对象 | 基础偏移量:正值向上偏移,负值向下偏移,默认0(不偏移) |
.obliqueness | 包含浮点数的NSNumber对象 | 字体倾斜 :正值向右倾斜,负值向左倾斜, 默认0(不倾斜) |
.expansion | 包含浮点数的NSNumber对象 | 文本扁平化:正值横向拉伸,负值横向压缩,默认0(不拉伸) |
2、属性详解及应用
NSMutableAttributedString 是 NSAttributedString 的子类,一般来说我比较习惯使用NSMutableAttributedString来实现富文本。
2.1 NSAttributedString.Key.font --字体大小
let title = "联系客服"
let text = "遇到问题?您可以\(title)"
//1、创建NSMutableAttributeString实例
let attributeString = NSMutableAttributedString(string: text)
//2、添加属性
attributeString.addAttribute(.font, value: UIFont.systemFont(ofSize: 30), range: NSMakeRange(text.count - title.count, title.count))
//3、赋值
label.attributedText = attributeString
2.2 NSAttributedString.Key.paragraphStyle – 文本字、行间距,对齐等
let title = "联系客服"
let text = "遇到问题?您可以\(title)"
//1、创建NSMutableAttributeString实例
let attributeString = NSMutableAttributedString(string: text)
//2、添加属性
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center //居中对齐
attributeString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, text.count))
//3、赋值
label.attributedText = attributeString
2.3 NSAttributedString.Key.foregroundColor – 字体颜色
let title = "联系客服"
let text = "遇到问题?您可以\(title)"
//1、创建NSMutableAttributeString实例
let attributeString = NSMutableAttributedString(string: text)
//2、添加属性
attributeString.addAttribute(.foregroundColor, value: UIColor.orange, range: NSMakeRange(text.count-title.count, title.count))
//3、赋值
label.attributedText = attributeString
2.4 NSAttributedString.Key.backgroundColor – 背景色
let title = "联系客服"
let text = "遇到问题?您可以\(title)"
//1、创建NSMutableAttributeString实例
let attributeString = NSMutableAttributedString(string: text)
//2、添加属性
attributeString.addAttribute(.backgroundColor, value: UIColor.orange, range: NSMakeRange(text.count-title.count, title.count))
//3、赋值
label.attributedText = attributeString
2.5 NSAttributedString.Key.ligature – 连字符
ios 中有 0 和 1 两个值:0表示没有连字符,而1是默认的连字符。(一般对连笔写的英文有效, 中文即使设置了连字符也很难表现出来)。
2.6 NSAttributedString.Key.kern – 字符间距
正值间距加宽,负值间距变窄,0表示默认效果
let title = "联系客服"
let text = "遇到问题?您可以\(title)"
//1、创建NSMutableAttributeString实例
let attributeString = NSMutableAttributedString(str