ios-在Swi中将HTML转换为纯文本
我正在开发一个简单的RSS Reader应用程序,作为Xcode中的初学者项目。 目前,我已设置它解析提要,并放置标题,发布日期,描述和内容,并将其显示在WebView中。
我最近决定在用于选择帖子的TableView中显示说明(或内容的删节版本)。 但是,这样做时:
cell.textLabel?.text = item.title?.uppercaseString
cell.detailTextLabel?.text = item.itemDescription //.itemDescription is a String
它显示了帖子的原始HTML。
我想知道如何仅将TableView的详细UILabel转换为纯HTML。
谢谢!
8个解决方案
198 votes
您可以添加此扩展名以将html代码转换为常规字符串:
编辑/更新:
讨论不应从后台调用HTML导入器 线程(即,选项字典包括带有 的值)。 它将尝试与主线程同步,失败, 和超时。 从主线程调用它有效(但仍然可以 如果HTML包含对外部资源的引用,则会超时 应该不惜一切代价避免)。 HTML导入机制的目的是 用于实现诸如markdown之类的东西(即文本样式, 颜色等等),不适用于常规HTML导入。
Xcode 9•Swift 4
extension Data {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
extension String {
var html2AttributedString: NSAttributedString? {
return Data(utf8).html2AttributedString
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
Xcode 8.3•Swift 3.1
extension String {
var html2AttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: Data(utf8), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print("error:", error)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
cell.detailTextLabel?.text = item.itemDescription.html2String
Leo Dabus answered 2019-10-11T09:44:17Z
3 votes
斯威夫特4,Xcode 9
extension String {
var utfData: Data {
return Data(utf8)
}
var attributedHtmlString: NSAttributedString? {
do {
return try NSAttributedString(data: utfData,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)
} catch {
print("Error:", error)
return nil
}
}
}
extension UILabel {
func setAttributedHtmlText(_ html: String) {
if let attributedText = html.attributedHtmlString {
self.attributedText = attributedText
}
}
}
Suhit Patil answered 2019-10-11T09:44:40Z
2 votes
这是我建议的答案。 如果要放入函数中,请不要使用扩展名。
func decodeString(encodedString:String) -> NSAttributedString?
{
let encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)!
do {
return try NSAttributedString(data: encodedData, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
并调用该函数并将NSAttributedString转换为String
let attributedString = self.decodeString(encodedString)
let message = attributedString.string
Danboz answered 2019-10-11T09:45:10Z
1 votes
请使用此代码测试detailTextLabel:
var attrStr = NSAttributedString(
data: item.itemDescription.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true),
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
cell.detailTextLabel?.text = attrStr
Altimir Antonov answered 2019-10-11T09:45:34Z
1 votes
在swift3中尝试此解决方案
extension String{
func convertHtml() -> NSAttributedString{
guard let data = data(using: .utf8) else { return NSAttributedString() }
do{
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
}catch{
return NSAttributedString()
}
}
}
使用
self.lblValDesc.attributedText = str_postdescription.convertHtml()
Hardik Thakkar answered 2019-10-11T09:46:04Z
0 votes
我使用了Danboz答案,只是将其更改为返回一个简单的String(不是富文本字符串):
static func htmlToText(encodedString:String) -> String?
{
let encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)!
do
{
return try NSAttributedString(data: encodedData, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil).string
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
对我来说,它就像一种魅力,谢谢Danboz
Shaybc answered 2019-10-11T09:46:34Z
0 votes
let content = givenString // html included string
let attrStr = try! NSAttributedString(data: content.data(using: String.Encoding.unicode, allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],documentAttributes: nil)
self.labelName.attributedText = attrStr
shahana mh answered 2019-10-11T09:46:51Z
0 votes
Swift4.0扩展
extension String {
var html2AttributedString: String? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil).string
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
}
Maulik Patel answered 2019-10-11T09:47:14Z