作为Xcode的初学者,我正在开发一个简单的RSS阅读器应用程序,目前我已经设置好了,它会解析提要,放置标题、发布日期、描述和内容,并在WebView中显示。
我最近决定在用来选择帖子的TableView中显示描述(或内容的截断版本)。但是,在这样做时:
cell.textLabel?.text = item.title?.uppercaseString
cell.detailTextLabel?.text = item.itemDescription //.itemDescription is a String
它显示帖子的原始HTML。
我想知道如何将HTML转换为纯文本,只是TableView的详细UILabel。
谢谢!
9条答案
按热度按时间mctunoxg1#
您可以添加此扩展以将html代码转换为常规字符串:
编辑/更新:
讨论不应从后台线程调用HTML导入程序(即选项字典包含值为html的documentType)。它将尝试与主线程同步,失败并超时。从主线程调用它可以工作(但是如果HTML包含对外部资源的引用,应该不惜一切代价避免)。HTML导入机制是为了实现类似Markdown的东西(即文本样式、颜色等),而不是为了一般的HTML导入。
快码11.4版·斯威夫特5.2版
9q78igpj2#
斯威夫特4,Xcode 9
ygya80vv3#
这是我建议的答案。如果你想把函数放在里面,就不要用扩展。
然后调用该函数并将NSAttributedString强制转换为String
xdnvmnnf4#
请使用以下代码测试detailTextLabel:
lvmkulzt5#
在swift3中试用此解决方案
使用
o75abkj46#
Swift4.0扩展
qcuzuvrc7#
我用过Danboz answer,只是把它改为返回一个简单的字符串(不是富文本字符串):
对我来说,它的工作就像一个魅力,谢谢丹博兹
b1uwtaje8#
jrcvhitl9#
雨燕5号 *
下面是一个基于字符串扩展的紧凑解决方案:
用法:
您也可以根据需要保留属性化字符串ofc