Ich arbeite an einer einfachen RSS-Reader-App als Anfängerprojekt in Xcode. Derzeit habe ich festgelegt, dass der Feed analysiert wird, Titel, Veröffentlichungsdatum, Beschreibung und Inhalt platziert und in einem WebView angezeigt wird.
Ich habe vor kurzem beschlossen, die Beschreibung (oder eine abgeschnittene Version des Inhalts) in der TableView anzuzeigen, die zum Auswählen eines Beitrags verwendet wird. Wenn Sie dies jedoch tun:
cell.textLabel?.text = item.title?.uppercaseString
cell.detailTextLabel?.text = item.itemDescription //.itemDescription is a String
Es zeigt den rohen HTML-Code des Beitrags.
Ich würde gerne wissen, wie man HTML-Code nur für das ausführliche UILabel von TableView in einfachen Text konvertiert.
Vielen Dank!
Sie können diese Erweiterung hinzufügen, um Ihren HTML-Code in eine reguläre Zeichenfolge zu konvertieren:
bearbeiten/Aktualisieren:
Diskussion Der HTML-Importer sollte nicht aus einem Hintergrund aufgerufen werden Thread (dh das Optionswörterbuch enthält documentType mit einem -Wert von html). Es wird versucht, sich mit dem Haupt-Thread zu synchronisieren. und auszeit. Das Aufrufen aus dem Haupt-Thread funktioniert (kann jedoch immer noch ___ Zeitlimit überschreiten, wenn der HTML-Code Verweise auf externe Ressourcen enthält, die ohnehin vermieden werden sollten). Der HTML-Importmechanismus ist zum Implementieren von Markdown (z. B. Textstilen, Farben usw.), nicht für den allgemeinen HTML-Import.
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
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
}
}
}
Hier ist meine vorgeschlagene Antwort. Anstelle der Erweiterung, wenn Sie eine Funktion einfügen möchten.
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
}
}
Rufen Sie diese Funktion auf und wandeln Sie NSAttributedString in String um
let attributedString = self.decodeString(encodedString)
let message = attributedString.string
Bitte testen Sie mit diesem Code das detailTextLabel:
var attrStr = NSAttributedString(
data: item.itemDescription.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true),
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
cell.detailTextLabel?.text = attrStr
Versuchen Sie diese Lösung in 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()
}
}
}
Benutzen
self.lblValDesc.attributedText = str_postdescription.convertHtml()
Swift4.0-Erweiterung
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
}
}
}
ich habe die Antwort von Danboz verwendet, nur geändert, um einen einfachen String (keinen Rich-Text-String) zurückzugeben:
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
}
}
für mich funktioniert das wie ein Zauber, danke Danboz
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