NSAttributedString 初始化
NSAttributedString.init(string: "TEST", attributes: [:])
attributes 类型为 [NSAttributedStringKey: Any]
下面开始介绍 NSAttributeStringKey 每一个的作用
如果 NSAttributeStringKey 对应的类型传错参数,文本会为透明色
常用
NSAttributedString.Key.font 字体
NSAttributedString.Key.foregroundColor 字体颜色
NSAttributedString.Key.backgroundColor
文本的背景颜色: UIColor
self.textView.attributedText = NSAttributedString.init(string: "TEST", attributes: [
NSAttributedString.Key.backgroundColor: UIColor.gray
])
NSAttributedString.Key.ligature
表示某些连在一起的字符: NSNumber,默认 0 不连体,1为连体
只有某些字符且某些字体才会发生连体
字体为 PingFangSC-Medium 和 PingFangSC-Semibold 等,会发生连体
self.textView.attributedText = NSAttributedString.init(string: "fiflfi", attributes: [
NSAttributedString.Key.ligature: 1,
NSAttributedString.Key.font: UIFont.init(name: "PingFangSC-Medium", size: 17)!
])
NSAttributedString.Key.kern
调整字横向间距
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.kern: 10
])
NSAttributedString.Key.strikethroughStyle
新增删除线: NSNumber
// NSNumber 为 NSUnderlineStyle 枚举定义的
NSUnderlineStyleNone: 0 // 无删除线
NSUnderlineStyleSingle: 1 // 单条删除线
NSUnderlineStyleThick: 2 // 粗一点的删除线
NSUnderlineStyleDouble: 9 // 两条删除线
/// 删除线颜色
NSAttributedString.Key.strikethroughColor: UIColor.black
NSAttributedString.Key.underlineStyle
下划线: NSNumber
// NSNumber 为 NSUnderlineStyle 枚举定义的
NSUnderlineStyleNone: 0 // 无删除线
NSUnderlineStyleSingle: 1 // 单条删除线
NSUnderlineStyleThick: 2 // 粗一点的删除线
NSUnderlineStyleDouble: 9 // 两条删除线
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.underlineStyle: 9,
])
/// 下划线颜色
NSAttributedString.Key.underlineColor: UIColor.black
NSAttributedString.Key.strokeWidth
文字描边: NSNumber
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.strokeWidth: 2
])
/// 文字描边颜色
NSAttributedString.Key.strokeColor: UIColor.black
NSAttributedString.Key.shadow
文字阴影: NSShadow
let shadow = NSShadow.init()
shadow.shadowColor = UIColor.red
shadow.shadowOffset = CGSize.init(width: 0, height: 2)
shadow.shadowBlurRadius = 5
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.shadow: shadow
])
NSAttributedString.Key.textEffect
图版印刷效果: NSAttributedString.TextEffectStyle,目前只有这一个效果
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.textEffect: NSAttributedString.TextEffectStyle.letterpressStyle.rawValue
])
NSAttributedString.Key.baselineOffset
文字基线的偏移量: NSNumber
NSAttributedString.Key.writingDirection
文字书写方向: NSNumber 数组
// NSNumber 为 NSWritingDirection 枚举定义的
NSUnderlineStyleNone:
NSAttributedString.Key.link
添加超链接,点击文本可跳转浏览器打开 URL: URL
self.textView.attributedText = NSAttributedString.init(string: "TEST NONE", attributes: [
NSAttributedString.Key.link: URL.init(string: "https://www.baidu.com")!
])
NSAttributedString.Key.obliqueness
文字倾斜: NSNumber
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.obliqueness: -1 // < 0 向左,> 0 向右
])
NSAttributedString.Key.expansion
文字的压缩和拉伸: NSNumber
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.expansion: 1 // < 0 压缩,> 0 拉伸
])
NSTextAttachment
用于图文混编
let attachment = NSTextAttachment.init()
attachment.image = UIImage.init(named: "icon_bluetooth_20")!
/// 可调整图片 y 坐标, > 0 往上移动,< 0 往下移动
attachment.bounds = CGRect.init(x: 0, y: -2, width: 17, height: 17)
let mutableAttrString = NSMutableAttributedString.init(attachment: attachment)
mutableAttrString.append(NSAttributedString.init(string: "TEST TEST"))
self.textView.attributedText = mutableAttrString
NSAttributedString.Key.paragraphStyle
用于编辑段落属性: NSParagraphStyle
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.lineSpacing = 10 // 行间距
paragraphStyle.paragraphSpacing = 10 // 段间距
paragraphStyle.alignment = .left // 两端对齐方式
paragraphStyle.firstLineHeadIndent = 10 // 首行缩进
paragraphStyle.headIndent = 10 // 整体缩进,首行除外
paragraphStyle.tailIndent = -30 // 尾部缩进, < 0 abs(tailIndent) 尾部距离右边的距离,> 0 abs(tailIndent) 尾部距离左边的距离
/*
NSLineBreakMode
例句: Happiness is a state of mind.
.byWordWrapping = 0, // 默认,单词换行,Happiness is a state\n of mind
.byCharWrapping, // 字符换行,Happiness is a state o\nf mind
.byClipping, // 直接剪掉,Happiness is a state
.byTruncatingHead, // "...state of mind"
.byTruncatingTail, // "Happiness is a..."
.byTruncatingMiddle // "Happiness is...of mind"
*/
paragraphStyle.lineBreakMode = .byCharWrapping // 断行方式,文本一行不够展示如何处理
paragraphStyle.minimumLineHeight = 10 // 最小行高
paragraphStyle.maximumLineHeight = 10 // 最大行高
paragraphStyle.baseWritingDirection = .rightToLeft // 书写方向
paragraphStyle.lineHeightMultiple = 1.7 // 行高系数 > 0,在最大行高和最小行高约束之前进行计算
paragraphStyle.paragraphSpacingBefore = 10 // 上一段断末和本段段首之间的距离
paragraphStyle.hyphenationFactor = // 自动断字,hyphen(短横 - )将单词断开, 例如 impeachment 断为 im-peachment
self.textView.attributedText = NSAttributedString.init(string: "fiflfi", attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle
])