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标签,并给出输出
。
但是,如果我想增加字体大小并取消注释addAttribute
,字体大小会增加但HTML不会呈现。
我能得到属性串既字体大小和HTML渲染?
这是正常的行为。斜体和粗体在UIFont中(以及在NSFontAttributeName中)。你需要迭代你的NSAttributedString的NSFontAttributeName这里有一个好的开始:http://stackoverflow.com/a/41413014/1801544(马特的答案,但不是像格鲁吉亚一样创建一个新字体,而是使用与之前相同的字体)。 – Larme
谢谢,马特的答案奏效。 – khanHussain