
let ?errStr = "訂課成功\r\n開課前2小時(shí)內(nèi)不允許取消訂課"
let strc = NSMutableAttributedString.init(string: errStr)
//間距
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.lineSpacing = 10
paragraphStyle.alignment = .center
strc.addAttributes([NSAttributedStringKey.paragraphStyle : paragraphStyle], range: NSRange.init(location: 0, length: strc.length))
strc.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], range: NSRange.init(location: 0, length: 4))
let str1M = NSAttributedString.init(string: "開課前2小時(shí)內(nèi)不允許取消訂課", attributes: [NSAttributedStringKey.foregroundColor:UIColor.yellow,NSAttributedStringKey.font:UIFont.systemFont(ofSize: 13)])
strc.replaceCharacters(in: NSRange.init(location: 6, length: 14), with: str1M)
最終得到的 strc 就是 一個(gè)可控各部分文字顏色 換行 對齊 的富文本