本文只作自用筆記,不喜勿噴,誠謝糾錯。
//str為后台獲取的html文本,里面包括了字體的顏色和相關屬性設置,我們獲取到后台數據后用下面的代碼進行解析
let attribstr = try! NSAttributedString.init(data:(str?.data(using: String.Encoding.unicode))! , options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
//給label(UILabel)進行屬性替換
label.attributedText = attribstr
然后顯示label就可以看到文字效果了,可以由后台動態控制文本顏色。