Swift-更改具有自己样式的 HTML 字符串上的字体 [英] Swift- Change font on an HTML string that has its own Styles
问题描述
我正在动态地从 Wordpress API 获取 HTML 字符串并将其解析为属性字符串以在我的应用程序中显示它.由于字符串有自己的样式,它显示不同的字体和大小,这会影响我们的设计选择.
I am dinamically getting an HTML string from a Wordpress API and parsing it into an Attributed String to show it in my app. Since the string has its own styles, it shows different fonts and sizes, something that is affecting our design choices.
我想要做的是更改整个属性字符串的字体及其大小.
What I want to do is change the font and its size on the whole attributed string.
我尝试在属性字符串的选项中这样做,但它什么也没做:
I tried doing so in the options of the attributed string, but it does nothing:
let attributedT = try! NSAttributedString(
data: nContent!.decodeHTML().data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName: UIFont(name: "Helvetica", size: 16.0)!],
documentAttributes: nil)
contentLbl.attributedText = attributedT
有人对如何实现这一目标有任何想法吗?
Does anybody have any ideas on how to achieve this?
附言我知道我可以在字符串的开头或结尾添加一个 CSS 标签,但这会覆盖其中的其他样式吗?另外,如果这是一个有效的解决方案,您能否提供一个示例来说明如何做到这一点?
P.S. I know that I could add a CSS tag to the beginning or end of the string, but would this override other styles in it? Also, if this is a valid solution, could you please provide a sample on how to do it?
推荐答案
setAttributes 将重置 HTML 中的所有属性.我写了一个扩展方法来避免这种情况:
The setAttributes will reset all the attributes from HTML. I wrote an extension method to avoid this:
Swift 4
public convenience init?(HTMLString html: String, font: UIFont? = nil) throws {
let options : [NSAttributedString.DocumentReadingOptionKey : Any] =
[NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]
guard let data = html.data(using: .utf8, allowLossyConversion: true) else {
throw NSError(domain: "Parse Error", code: 0, userInfo: nil)
}
if let font = font {
guard let attr = try? NSMutableAttributedString(data: data, options: options, documentAttributes: nil) else {
throw NSError(domain: "Parse Error", code: 0, userInfo: nil)
}
var attrs = attr.attributes(at: 0, effectiveRange: nil)
attrs[NSAttributedStringKey.font] = font
attr.setAttributes(attrs, range: NSRange(location: 0, length: attr.length))
self.init(attributedString: attr)
} else {
try? self.init(data: data, options: options, documentAttributes: nil)
}
}
测试样本:
let html = "<html><body><h1 style="color:red;">html text here</h1></body></html>"
let font = UIFont.systemFont(ofSize: 16)
var attr = try NSMutableAttributedString(HTMLString: html, font: nil)
var attrs = attr?.attributes(at: 0, effectiveRange: nil)
attrs?[NSAttributedStringKey.font] as? UIFont
// print: <UICTFont: 0x7ff19fd0a530> font-family: "TimesNewRomanPS-BoldMT"; font-weight: bold; font-style: normal; font-size: 24.00pt
attr = try NSMutableAttributedString(HTMLString: html, font: font)
attrs = attr?.attributes(at: 0, effectiveRange: nil)
attrs?[NSAttributedStringKey.font] as? UIFont
// print: <UICTFont: 0x7f8c0cc04620> font-family: ".SFUIText"; font-weight: normal; font-style: normal; font-size: 16.00pt
这篇关于Swift-更改具有自己样式的 HTML 字符串上的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!