在Swift中将HTML转换为纯文本

时间:2015-01-24 09:36:30

标签: ios swift uitableview

我正在开发一个简单的RSS阅读器应用程序作为Xcode中的初学者项目。我目前设置它解析feed,并放置标题,发布日期,描述和内容,并将其显示在WebView中。

我最近决定在用于选择帖子的TableView中显示描述(或内容的截断版本)。但是,这样做的时候:

cell.textLabel?.text = item.title?.uppercaseString
cell.detailTextLabel?.text = item.itemDescription //.itemDescription is a String

显示帖子的原始HTML。

我想知道如何将HTML转换为纯文本,仅用于TableView的详细UILabel。

谢谢!

8 个答案:

答案 0 :(得分:204)

您可以添加此扩展程序,将您的html代码转换为常规字符串:

编辑/更新:

  

讨论不应从后台调用HTML导入程序   thread(即,options字典包含带有的documentType   html的值)。它将尝试与主线程同步,失败,   和超时。从主线程调用它可以工作(但仍然可以   如果HTML包含对外部资源的引用,则超时   应该不惜一切代价避免)。 HTML导入机制是指   用于实现降价(即文本样式,   颜色等),不适用于一般的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

答案 1 :(得分:3)

Swift 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
      } 
   }
}

答案 2 :(得分:2)

这是我建议的答案。而不是扩展,如果你想放入内部功能。

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

答案 3 :(得分:1)

请使用此代码测试detailTextLabel:

var attrStr = NSAttributedString(
        data: item.itemDescription.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true),
        options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil,
        error: nil)
cell.detailTextLabel?.text = attrStr

答案 4 :(得分:1)

在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()

答案 5 :(得分:0)

我使用过Danboz的答案,只是将其改为返回一个简单的字符串(不是一个富文本字符串):

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

答案 6 :(得分:0)

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    

答案 7 :(得分:0)

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
    }
  }
}