在 UITextView 中显示 HTML 内容

She*_*eem 4 ios swift

我想使用 swift 在 UITextView 控件中显示字符串 HTML。我找到了一些代码并尝试了一下:

     do {
        let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
    } catch {
        print(error)
    }
Run Code Online (Sandbox Code Playgroud)

但我给出了一些我无法理解或找到任何解决方案的错误,其中指出,

无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey”

我需要弄清楚为什么我会遇到这个错误。任何解决方案或替代代码?其他解决方案在objective-c中,如果他们有答案,他们会给出同样的错误,我试过很多只是因为旧版本。

Kul*_*eep 7

尝试这个

var attrStr = try! NSAttributedString(
            data: "<b><i>text</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
            options:[NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
        YOUR_TEXTVIEW.attributedText = attrStr
Run Code Online (Sandbox Code Playgroud)