在Swift中将HTML转换为纯文本

suzh9iv8  于 2023-03-16  发布在  Swift
关注(0)|答案(9)|浏览(523)

作为Xcode的初学者,我正在开发一个简单的RSS阅读器应用程序,目前我已经设置好了,它会解析提要,放置标题、发布日期、描述和内容,并在WebView中显示。
我最近决定在用来选择帖子的TableView中显示描述(或内容的截断版本)。但是,在这样做时:

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

它显示帖子的原始HTML。
我想知道如何将HTML转换为纯文本,只是TableView的详细UILabel。
谢谢!

mctunoxg

mctunoxg1#

您可以添加此扩展以将html代码转换为常规字符串:
编辑/更新:
讨论不应从后台线程调用HTML导入程序(即选项字典包含值为html的documentType)。它将尝试与主线程同步,失败并超时。从主线程调用它可以工作(但是如果HTML包含对外部资源的引用,应该不惜一切代价避免)。HTML导入机制是为了实现类似Markdown的东西(即文本样式、颜色等),而不是为了一般的HTML导入。

快码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
9q78igpj

9q78igpj2#

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

ygya80vv3#

这是我建议的答案。如果你想把函数放在里面,就不要用扩展。

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
xdnvmnnf

xdnvmnnf4#

请使用以下代码测试detailTextLabel:

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

lvmkulzt5#

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

o75abkj46#

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

qcuzuvrc7#

我用过Danboz answer,只是把它改为返回一个简单的字符串(不是富文本字符串):

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

对我来说,它的工作就像一个魅力,谢谢丹博兹

b1uwtaje

b1uwtaje8#

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
jrcvhitl

jrcvhitl9#

雨燕5号 *

下面是一个基于字符串扩展的紧凑解决方案:

import UIKit

extension String {
    var attributedHtmlString: NSAttributedString? {
        try? NSAttributedString(
            data: Data(utf8),
            options: [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ],
            documentAttributes: nil
        )
    }
}

用法:

let html = "hello <br><br/> <b>world</b>"
if let attributedText = html.attributedHtmlString {
    print(attributedText.string) // "hello \n\nworld\n"
}

您也可以根据需要保留属性化字符串ofc

相关问题