HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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:&#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来破解它.

大佬总结

以上是大佬教程为你收集整理的HTML到NSAttributedString和NSAttributedString到HTML全部内容,希望文章能够帮你解决HTML到NSAttributedString和NSAttributedString到HTML所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:html