在项目中你遇到一个需要显示html的需求,但直接显示的话,会有显示一些不需要的符号,需做一些处理
个人喜好用扩展
extension String {
var htmlAttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: Data(utf8),
options: [.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
} catch {
print("error: ", error)
return nil
}
}
var htmlString: String {
return htmlAttributedString?.string ?? self
}
}
来源:oschina
链接:https://my.oschina.net/wshzj/blog/4307840