iOS - NSAttributedString

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
])
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市幼东,隨后出現(xiàn)的幾起案子臂容,更是在濱河造成了極大的恐慌,老刑警劉巖根蟹,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脓杉,死亡現(xiàn)場離奇詭異,居然都是意外死亡简逮,警方通過查閱死者的電腦和手機(jī)球散,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來散庶,“玉大人蕉堰,你說我怎么就攤上這事”辏” “怎么了屋讶?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長须教。 經(jīng)常有香客問我丑婿,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任羹奉,我火速辦了婚禮秒旋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘诀拭。我一直安慰自己迁筛,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布耕挨。 她就那樣靜靜地躺著细卧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪筒占。 梳的紋絲不亂的頭發(fā)上贪庙,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機(jī)與錄音翰苫,去河邊找鬼止邮。 笑死,一個胖子當(dāng)著我的面吹牛奏窑,可吹牛的內(nèi)容都是我干的导披。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼埃唯,長吁一口氣:“原來是場噩夢啊……” “哼撩匕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起墨叛,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤止毕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后漠趁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扁凛,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年棚潦,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片膝昆。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡丸边,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出荚孵,到底是詐尸還是另有隱情妹窖,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布收叶,位于F島的核電站骄呼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蜓萄,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一隅茎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嫉沽,春花似錦辟犀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至玻佩,卻和暖如春出嘹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背咬崔。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工税稼, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人刁赦。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓娶聘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甚脉。 傳聞我的和親對象是個殘疾皇子丸升,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容