加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

HTML到NSAttributedString和NSAttributedString到HTML

发布时间:2020-12-14 23:25:17 所属栏目:资源 来源:网络整理
导读:我有NSAttributedString的问题… 我想将HTML字符串转换为NSAttributedString,然后处理NSAttributedString(更改一些颜色,fontsize,fontfamily,background-,foreground-Color),然后转换回来自NSAttributedString的纯HTML文本. 转换不是问题,但每次我将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:&#039;andale mono&#039;,times;'>Dies</span> <span style='font-family:&#039;comic sans ms&#039;,sans-serif;'>ist</span> <strong><span style='font-family:&#039;andale mono&#039;,sans-serif;';>eine</span></strong> <em>formatierte</em> <span style='text-decoration:underline;'>Karte</span>&#160;<span style='font-size:16px;'>die</span> <span style='background-color:#ffff00;'>es</span> zu &#220;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来破解它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读