Ios 在UITextView中显示HTML内容

Ios 在UITextView中显示HTML内容,ios,swift,Ios,Swift,我想使用swift在UITextView控件中显示字符串HTML。我找到了一些代码并尝试了: do { let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType],

我想使用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)
    }
但我给出了一些我无法理解或找到任何解决方案的错误,声明为


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

我需要弄清楚我为什么会犯这个错误。有解决方案或替代代码吗?
其他解决方案在objective-c中,如果它们有答案,它们给出的错误与我在旧版本中尝试过的错误相同。

选项更改为

let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
因此,它应该是:

 do {
    let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
    let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: options, documentAttributes: nil)
} catch {
    print(error)
}
试试这个

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
var attrStr=try!NSAttribute字符串(
数据:“文本”。数据(使用:String.Encoding.unicode,allowossyconversion:true)!,
选项:[NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.documentType.html],documentAttributes:nil)
您的\u TEXTVIEW.attributedText=attrStr

使用这个,它对我来说正常工作

let attrStr = try! NSAttributedString(
            data: htmlText.data(using: .unicode, allowLossyConversion: true)!,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
            NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
            documentAttributes: nil)
试试这个

   let htmlString = "<html><body> Some html string </body></html>";

    do {
        let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)

        self.txView.attributedText  = str

    } catch {
        print(error)
    }
让htmlString=“一些html字符串”;
做{
让str=try NSAttributedString(数据:htmlString.data(使用:String.Encoding.unicode,allowLossyConversion:true)!,选项:[NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.documentType.html],documentAttributes:nil)
self.txView.attributeText=str
}抓住{
打印(错误)
}

Swift 4的可能重复引入了新的“键”:NSDocumentTypeDocumentAttribute vs
NSAttributedString。someKey
您使用的是旧的,以及更像Objective-C one的内容。无法将类型为“NSAttributeString.DocumentAttributeKey”的值转换为预期的字典键类型“NSAttributeString.DocumentReadingOptionKey”,已在讨论中解释过。