我希望把一个HTML字符串转换为NSAttributedString,然后对这个字符串进行修改(比如:更改颜色、字体大小、字体类型、背景色和前景色),最后再把字符串从NSAttributedString转回普通的HTML格式。
转换(Conversion)并不是问题,但是每次我将HTML转换为NSAttributedString,再转回去时,字体大小会变得越来越大……
样例代码:
这是游乐场的结果。你可以看到第二个文本比第一个文本大,但我没有在任何地方改变字体大小。
这是一个错误还是我需要设置固定字体大小?
更新:
我接受@Lou Franco的答案。我不知道为什么NSAS要将px转换为pt然后再转回来,但这是我的解决方法:
这样做的唯一缺点是您会在NSAS中失去不同的文本高度... 如果有人有解决方案或更好的解决方法,请随时发布您的答案。
转换(Conversion)并不是问题,但是每次我将HTML转换为NSAttributedString,再转回去时,字体大小会变得越来越大……
样例代码:
// 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()
这是游乐场的结果。你可以看到第二个文本比第一个文本大,但我没有在任何地方改变字体大小。
这是一个错误还是我需要设置固定字体大小?
更新:
我接受@Lou Franco的答案。我不知道为什么NSAS要将px转换为pt然后再转回来,但这是我的解决方法:
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
}
这样做的唯一缺点是您会在NSAS中失去不同的文本高度... 如果有人有解决方案或更好的解决方法,请随时发布您的答案。