HTML zu NSAttributedString und NSAttributedString zu HTML
Ich habe ein Problem mit NSAttributedString ...
Ich möchte ein @ konvertierHTML-Zeichenfolge in einen NSAttributedString und dann arbeite am NSAttributedString (ändere einige Farben, Schriftgröße, Schriftfamilie, Hintergrund-, Vordergrundfarbe) und konvertieren Sie dann einfachen HTML-Text aus dem NSAttributedString zurück.
Converting ist kein Problem, aber bei jedem Konvertieren von HTML in NSAS und zurück dasfontsize wird immer größer?!
Hier ist ein Bild und ein Quellcode von meinem Spielplatz.
// Playground - noun: a place where people can play
// NSAS: - NSAttributedString
import UIKit
class Wrapper {
//MARK: fields
let apiHtml = "<div style='font-size: 18px'><span style='font-family:'andale mono', times;'>Dies</span> <span style='font-family:'comic sans ms', sans-serif;'>ist</span> <strong><span style='font-family:'andale mono', sans-serif;';>eine</span></strong> <em>formatierte</em> <span style='text-decoration:underline;'>Karte</span> <span style='font-size:16px;'>die</span> <span style='background-color:#ffff00;'>es</span> zu Übernehmen gilt</div>"
var newGeneratedHtml = ""
var textView : UITextView!
//MARK: constructor
init() {
//init textview
textView = UITextView(frame: CGRectMake(0, 0, 500, 300))
//convert html into NSAS and set it to textview
if let attributedText = getAttributedTextFromApiHtmlString(apiHtml) {
textView.attributedText = attributedText
}
//get html text from textfields NSAS
if let htmlText = getHtmlTextFromTextView() {
newGeneratedHtml = htmlText
println(htmlText)
}
//set the converted html from textfields NSAS
if let attributedText = getAttributedTextFromApiHtmlString(newGeneratedHtml) {
textView.attributedText = attributedText
}
//get html text from textfields NSAS
if let htmlText = getHtmlTextFromTextView() {
newGeneratedHtml = htmlText
println(htmlText)
}
}
//MARK: methods
func getAttributedTextFromApiHtmlString(text : String) -> NSAttributedString? {
if let attributedText = NSAttributedString(data: text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil, error: nil) {
return attributedText
}
return nil
}
func getHtmlTextFromTextView() -> String? {
let attributedTextFromTextView = textView.attributedText
if let htmlData = attributedTextFromTextView.dataFromRange(NSMakeRange(0, attributedTextFromTextView.length), documentAttributes: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], error: nil) {
if let htmlString = NSString(data: htmlData, encoding: NSUTF8StringEncoding) {
return htmlString
}
}
return nil
}
}
var w = Wrapper()
Hier ist das Spielfeld-Ergebnis, Sie können sehen, dass der zweite Text größer als der erste Text ist, aber ich habe die Schriftgröße nirgends geändert.
Ist das ein Fehler oder musste ich eine feste Schriftgröße festlegen?
Aber das Festlegen einer festen Schriftgröße ist nicht das, was ich wirklich will, weil es für jedes Zeichen unterschiedlich sein kann ...
AKTUALISIEREN
ch akzeptiere die Antwort von Louis Franco, weil er recht hat. Ich weiß nicht warum er konvertiert istpx
zupt
und zurück, aber hier ist meine Problemumgehung:
func getAttributedTextFromApiHtmlString(text : String) -> NSAttributedString? {
if let attributedText = NSAttributedString(data: text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil, error: nil) {
var res : NSMutableAttributedString = attributedText.mutableCopy() as NSMutableAttributedString
res.beginEditing()
var found : Bool = false;
res.enumerateAttribute(NSFontAttributeName, inRange:NSMakeRange(0, res.length) ,options:NSAttributedStringEnumerationOptions.allZeros, usingBlock: {(value:AnyObject!, range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) -> Void in
if ((value) != nil) {
let oldFont = value as UIFont;
let newFont = oldFont.fontWithSize(15)
res.removeAttribute(NSFontAttributeName, range:range)
res.addAttribute(NSFontAttributeName, value: newFont, range: range)
found = true
}
})
if !found {
// No font was found - do something else?
}
res.endEditing()
return res
}
return nil
}
Der einzige Nachteil ist, dass Sieverliere verschiedene Texthöhen in Ihrem AttributedString ....
Nun muss ich einen Weg finden, um die verschiedenen Text-Höhen beizubehalten ... Wenn jemand die Lösung hat oder es besser macht, kannst du gerne deine Antwort posten.