我想使用 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)
}
但我给出了一些我无法理解或找不到任何解决方案的错误,它表示为,
无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey”
我需要弄清楚为什么会出现此错误。任何解决方案或替代代码? 其他解决方案在 objective-c 中,如果他们有答案,他们会给出相同的错误,我尝试了很多,只是因为旧版本。
最佳答案
试试这个
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
关于ios - 在 UITextView 中显示 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47922200/