NSAttributedString 初始化
NSAttributedString.init(string: "TEST", attributes: [:])
attributes 類型為 [NSAttributedStringKey: Any]
下面開始介紹 NSAttributeStringKey 每一個的作用
如果 NSAttributeStringKey 對應(yīng)的類型傳錯參數(shù),文本會為透明色
常用
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.backgroundColor: UIColor.gray
NSAttributedString.Key.ligature
表示某些連在一起的字符: NSNumber植锉,默認(rèn) 0 不連體目代,1為連體
只有某些字符且某些字體才會發(fā)生連體
字體為 PingFangSC-Medium 和 PingFangSC-Semibold 等蝗羊,會發(fā)生連體
self.textView.attributedText = NSAttributedString.init(string: "fiflfi", attributes: [
NSAttributedString.Key.ligature: 1,
NSAttributedString.Key.font: UIFont.init(name: "PingFangSC-Medium", size: 17)!
])
NSAttributedString.Key.ligature: 1
NSAttributedString.Key.kern
調(diào)整字橫向間距
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.kern: 10
])
NSAttributedString.Key.kern: 10
NSAttributedString.Key.strikethroughStyle
新增刪除線: NSNumber
// NSNumber 為 NSUnderlineStyle 枚舉定義的
NSUnderlineStyleNone: 0 // 無刪除線
NSUnderlineStyleSingle: 1 // 單條刪除線
NSUnderlineStyleThick: 2 // 粗一點的刪除線
NSUnderlineStyleDouble: 9 // 兩條刪除線
/// 刪除線顏色
NSAttributedString.Key.strikethroughColor: UIColor.black
NSUnderlineStyleSingle
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.underlineStyle: 9
NSAttributedString.Key.strokeWidth
文字描邊: NSNumber
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.strokeWidth: 2
])
/// 文字描邊顏色
NSAttributedString.Key.strokeColor: UIColor.black
NSAttributedString.Key.strokeWidth: 2
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.shadow
NSAttributedString.Key.textEffect
圖版印刷效果: NSAttributedString.TextEffectStyle,目前只有這一個效果
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.textEffect: NSAttributedString.TextEffectStyle.letterpressStyle.rawValue
])
NSAttributedString.Key.textEffect
NSAttributedString.Key.baselineOffset
文字基線的偏移量: NSNumber
NSAttributedString.Key.writingDirection
文字書寫方向: NSNumber 數(shù)組
// NSNumber 為 NSWritingDirection 枚舉定義的
NSUnderlineStyleNone:
NSAttributedString.Key.link
添加超鏈接侧戴,點擊文本可跳轉(zhuǎn)瀏覽器打開 URL: URL
self.textView.attributedText = NSAttributedString.init(string: "TEST NONE", attributes: [
NSAttributedString.Key.link: URL.init(string: "https://www.baidu.com")!
])
NSAttributedString.Key.link
NSAttributedString.Key.obliqueness
文字傾斜: NSNumber
self.textView.attributedText = NSAttributedString.init(string: "TEST TEST", attributes: [
NSAttributedString.Key.obliqueness: -1 // < 0 向左,> 0 向右
])
NSAttributedString.Key.obliqueness: -1
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")!
/// 可調(diào)整圖片 y 坐標(biāo)霸饲, > 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
NSTextAttachment
NSAttributedString.Key.paragraphStyle
用于編輯段落屬性: NSParagraphStyle
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.lineSpacing = 10 // 行間距
paragraphStyle.paragraphSpacing = 10 // 段間距
paragraphStyle.alignment = .left // 兩端對齊方式
paragraphStyle.firstLineHeadIndent = 10 // 首行縮進(jìn)
paragraphStyle.headIndent = 10 // 整體縮進(jìn)盼玄,首行除外
paragraphStyle.tailIndent = -30 // 尾部縮進(jìn)贴彼, < 0 abs(tailIndent) 尾部距離右邊的距離,> 0 abs(tailIndent) 尾部距離左邊的距離
/*
NSLineBreakMode
例句: Happiness is a state of mind.
.byWordWrapping = 0, // 默認(rèn)埃儿,單詞換行器仗,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 // 行高系數(shù) > 0,在最大行高和最小行高約束之前進(jìn)行計算
paragraphStyle.paragraphSpacingBefore = 10 // 上一段斷末和本段段首之間的距離
paragraphStyle.hyphenationFactor = // 自動斷字剃斧,hyphen(短橫 - )將單詞斷開轨香, 例如 impeachment 斷為 im-peachment
self.textView.attributedText = NSAttributedString.init(string: "fiflfi", attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle
])