ios - Swift 4 解码 HTML 特殊字符
问题描述
我目前正在开发这个集成到 wordpress 网站的应用程序。App 获取 JSON 数据并显示在 TextView 中。
我面临 HTML 字符的问题。worpress 网站添加了 HTML 代码,例如当它在应用程序中显示时它不解码。
有没有办法解码html字符?
我目前正在使用以下代码:
descTextView.text = NewsContentViewController.newsDetail.desc
解决方案
尝试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.desc
is optional String
。
推荐阅读
- c - 记忆可能是问题吗?
- asp.net-core - 当我将身份添加到项目时,为什么我的 OAuth 提供程序无法正常工作?
- angular - 相同角度应用的两个不同域和路径
- ansible - Ansible:从环境变量中的路径复制文件
- typo3 - 扩展中的多个控制器
- java - 带有分隔符和值的拆分字符串数组
- tensorflow - 如何将数字列表作为单个特征传递给神经网络?
- javascript - 存储在变量中会提高性能吗?var, let, const 也会影响性能吗?
- javascript - 从 javascript 函数返回 json 数据
- http - OpenID 连接 - 在用户向我的服务器发出任何请求时,我是否需要在谷歌验证令牌?