美文网首页iOS
Swift Label中NSAttributedString显示

Swift Label中NSAttributedString显示

作者: KingWorld | 来源:发表于2021-08-25 10:12 被阅读0次
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 {
}

相关文章

网友评论

    本文标题:Swift Label中NSAttributedString显示

    本文链接:https://www.haomeiwen.com/subject/rpssiltx.html