美文网首页
Swift:在标签或textView或者UILabel中显示HT

Swift:在标签或textView或者UILabel中显示HT

作者: ShineYangGod | 来源:发表于2019-08-23 08:41 被阅读0次

    对于Swift 4:

    extension String {
        var htmlToAttributedString: NSAttributedString? {
            guard let data = data(using: .utf8) else { return NSAttributedString() }
            do {
                return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
            } catch {
                return NSAttributedString()
            }
        }
        var htmlToString: String {
            return htmlToAttributedString?.string ?? ""
        }
    }
    

    然后,只要您想将HTML文本放在UITextView或者UILabel中使用:

    label.attributedText = htmlText.htmlToString

    相关文章

      网友评论

          本文标题:Swift:在标签或textView或者UILabel中显示HT

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