2017-01-12 27 views
1

我无法使用NSMutableAttributedString来读取HTML字符串并增加它的字体大小。具有字体大小和HTML标记属性的NSMutableAttributedString

输入HTML:<b>Lorem ipsum</b> dolor sit <i>amet</i></h3>consectetuer</h3> adipiscing elit

而且NSMutableAttributedString转换如下:

let htmlData = input.data(using: .utf8)! 

let htmlAttrs = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:String.Encoding.utf8.rawValue] as [String : Any] 

do{ 
    let new = try NSMutableAttributedString(data: htmlData, options: htmlAttrs, documentAttributes: nil) 

    let tillEnd = NSRange(location: 0, length: new.length) 
    let attr:UIFont = UIFont.systemFont(ofSize: 25) 

    //new.addAttribute(NSFontAttributeName, value: attr, range: tillEnd) 

    textView.attributedText = new 
} 

它读取正确的HTML标签,并给出输出

HTML properly encoded

但是,如果我想增加字体大小并取消注释addAttribute,字体大小会增加但HTML不会呈现。

font size increase but html reading gone

我能得到属性串既字体大小和HTML渲染?

+1

这是正常的行为。斜体和粗体在UIFont中(以及在NSFontAttributeName中)。你需要迭代你的NSAttributedString的NSFontAttributeName这里有一个好的开始:http://stackoverflow.com/a/41413014/1801544(马特的答案,但不是像格鲁吉亚一样创建一个新字体,而是使用与之前相同的字体)。 – Larme

+0

谢谢,马特的答案奏效。 – khanHussain

回答

0

您可以使用风格跨度标签的属性,包括CSS与字体大小财产。