ios - 在 UITextView 中显示 HTML 内容

标签 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)
    }

但我给出了一些我无法理解或找不到任何解决方案的错误,它表示为,

无法将“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/

相关文章:

iphone - 无法在 iPhone 中实现单选按钮

ios - UIButton 边框在被选中时改变粗细

swift - 如何使用 Swift 在 Apple Watch 上添加下拉刷新功能

ios - 为 NSDate 添加 24 小时

ios - 如何在 UI 测试中加载动画后检查元素是否存在? swift

ios - 如何在 ios 中显示收件箱中的邮件数量

iphone - 分组的 UITableView 中未显示文本

ios - Crashlytics - EXC_BAD_ACCESS WebKitLegacy 崩溃 ios

ios - Estimote Beacon 加速度计更新时间

ios - 如何从 appDelegate 到 viewController