我有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 ….
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。