首页 > 解决方案 > Swift 4 解码 HTML 特殊字符

问题描述

我目前正在开发这个集成到 wordpress 网站的应用程序。App 获取 JSON 数据并显示在 TextView 中。

我面临 HTML 字符的问题。worpress 网站添加了 HTML 代码,例如当它在应用程序中显示时它不解码。

有没有办法解码html字符?

我目前正在使用以下代码:

descTextView.text = NewsContentViewController.newsDetail.desc

在此处输入图像描述

标签: iosswiftxcodeswift4

解决方案


尝试NSAttributedString像这样使用,

let str = NewsContentViewController.newsDetail.desc

if let data = str.data(using: .utf8) {
    do {
        let attrStr = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
        descTextView.attributedText = attrStr
        print(attrStr)
    } catch {
        print(error)
    }
}

在上面的代码中,处理 if NewsContentViewController.newsDetail.descis optional String


推荐阅读