ios 在 Swift 中将 HTML 转换为纯文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28124119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:29:05  来源:igfitidea点击:

Convert HTML to Plain Text in Swift

iosswiftuitableview

提问by Zaid Syed

I'm working on a simple RSS Reader app as a beginner project in Xcode. I currently have it set up that it parses the feed, and places the title, pub date, description and content and displays it in a WebView.

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

I recently decided to show the description (or a truncated version of the content) in the TableView used to select a post. However, when doing so:

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

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

It shows the raw HTML of the post.

它显示了帖子的原始 HTML。

I would like to know how to convert the HTML into plain text for just the TableView's detailed UILabel.

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

Thanks!

谢谢!

回答by Leo Dabus

You can add this extension to convert your html code to a regular string:

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

edit/update:

编辑/更新:

Discussion The HTML importer should not be called from a background thread (that is, the options dictionary includes documentType with a value of html). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.

讨论 不应从后台线程调用 HTML 导入程序(即,选项字典包含值为 html 的 documentType)。它将尝试与主线程同步,失败并超时。从主线程调用它是有效的(但如果 HTML 包含对外部资源的引用,则仍然会超时,应该不惜一切代价避免这种情况)。HTML 导入机制旨在实现诸如 Markdown 之类的东西(即文本样式、颜色等),而不是用于一般的 HTML 导入。

Xcode 11.4 ? Swift 5.2

Xcode 11.4?斯威夫特 5.2

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 { html2AttributedString?.string ?? "" }
}


extension StringProtocol {
    var html2AttributedString: NSAttributedString? {
        Data(utf8).html2AttributedString
    }
    var html2String: String {
        html2AttributedString?.string ?? ""
    }
}


cell.detailTextLabel?.text = item.itemDescription.html2String

回答by Suhit Patil

Swift 4, Xcode 9

斯威夫特 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
      } 
   }
}

回答by Danboz

Here is my suggested answer. Instead of extension, if you want to put inside function.

这是我建议的答案。而不是扩展,如果你想把里面的功能。

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

And call that function and cast NSAttributedString to String

并调用该函数并将 NSAttributedString 转换为 String

let attributedString = self.decodeString(encodedString)
let message = attributedString.string

回答by Altimir Antonov

Please test with this code for the detailTextLabel:

请使用此代码测试 detailTextLabel:

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

回答by Hardik Thakkar

Try this solution in swift3

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

To use

使用

self.lblValDesc.attributedText = str_postdescription.convertHtml()

回答by Maulik Patel

Swift4.0 Extension

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

回答by shahana mh

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    

回答by Shaybc

i have used Danboz answer, only changed it to return a simple String (not a rich text string):

我使用了 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
    }
}

for me, it works like a charm, thanks Danboz

对我来说,它就像一种魅力,谢谢 Danboz