Swift Label中NSAttributedString显示
2021-08-25 本文已影响0人
KingWorld
swift4.2 NSAttributedString显示HTML文本
func getAttributedString(str:String) -> NSAttributedString {
do {
return try NSAttributedString(data: str.data(using: String.Encoding.unicode, allowLossyConversion: true) ?? Data(),
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("")
return NSAttributedString()
}
}
swift5.3 NSAttributedString显示HTML文本
这个没有测试过
do {
if let data = str.data(using: .unicode) {
try NSAttributedString(data: data, options: [
NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html
], documentAttributes: nil)
}
} catch {
}