HTML到NSAttributedString和NSAttributedString到HTML
我有NSAttributedString的问题…
我想将HTML字符串转换为NSAttributedString,然后处理NSAttributedString(更改一些颜色,fontsize,fontfamily,background-,foreground-Color),然后转换回来自NSAttributedString的纯HTML文本. 转换不是问题,但每次我将HTML转换为NSAS并返回字体大小越来越大?! 这是我的游乐场的图片和源代码. // 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,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() 这是操场结果,你可以看到第二个文本比第一个文本大,但我没有改变任何地方的字体大小. 这是一个错误还是我必须设置修复字体大小? 但是设置一个固定的字体大小并不是我真正想要的,因为每个char都可能有所不同…… 更新: 我接受@Lou Franco的回答,因为他是对的.我不知道为什么他将px转换为pt并返回,但这是我的解决方法: func getAttributedTextFromApiHtmlString(text : String) -> NSAttributedString? { if let attributedText = NSAttributedString(data: text.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: false)!,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 } 唯一的缺点是你在AttributedString中丢失了不同的Text-Heights …. 现在我必须找到一种方法来保持不同的Text-Heights …… 解决方法
它可能会在往返过程中舍入错误.尝试使用整数磅值(使用pt而不是px)
好的,看看你的控制台输出,它正在将你的px翻译成pt,所以也许你可以通过获取转换后的HTML并将pt更改回px来破解它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |