OStack程序员社区-中国程序员成长平台

标题: ios - 将 HTML 转换为 NSAttributedString 时可以设置默认字体吗? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:37
标题: ios - 将 HTML 转换为 NSAttributedString 时可以设置默认字体吗?

我正在尝试在 UILabel 中显示 HTML,例如

NSString * htmlString = @"Some html string \n <font size=\"13\" color=\"red\">This is some text!</font>";

NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

现在我得到了一个属性字符串

attrStr 的打印说明: 一些html字符串

{

...

NSFont = "<UICTFont: 0x7f8240d7c310> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; **font-size: 12.00pt**";

}

这是一段文字!

{

...
NSFont = "<UICTFont: 0x7f8240d7aff0> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; **font-size: 37.00pt**";
}

似乎 font-size: 12.00pt 是默认大小,我想在不更改 HTML 源代码的情况下更改大小。该怎么做?



Best Answer-推荐答案


您可以通过一些编码来完成...您需要检查属性并更改属性字符串每个段的字体大小(如普通、斜体、粗体等...)。

extension String {
    func htmlAttributedString() -> NSMutableAttributedString {

            guard let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false)
                else { return NSMutableAttributedString() }

            guard let formattedString = try? NSMutableAttributedString(data: data,
                                                            options: [.documentType: NSAttributedString.DocumentType.html,
                                                                      .characterEncoding: String.Encoding.utf8.rawValue],
                                                            documentAttributes: nil )

                else { return NSMutableAttributedString() }

            return formattedString
    }

}


extension NSMutableAttributedString {

    func with(font: UIFont) -> NSMutableAttributedString {
        self.enumerateAttribute(NSAttributedStringKey.font, in: NSMakeRange(0, self.length), options: .longestEffectiveRangeNotRequired, using: { (value, range, stop) in
            let originalFont = value as! UIFont
            if let newFont = applyTraitsFromFont(originalFont, to: font) {
                self.addAttribute(NSAttributedStringKey.font, value: newFont, range: range)
            }
        })
        return self
    }

    func applyTraitsFromFont(_ f1: UIFont, to f2: UIFont) -> UIFont? {
        let originalTrait = f1.fontDescriptor.symbolicTraits

        if originalTrait.contains(.traitBold) {
            var traits = f2.fontDescriptor.symbolicTraits
            traits.insert(.traitBold)
            if let fd = f2.fontDescriptor.withSymbolicTraits(traits) {
                return UIFont.init(descriptor: fd, size: 0)
            }
        }
        return f2
    }
}

你会这样使用它

let formattedString = "text  <b>sometext</b>"
                        .htmlAttributedString()
                        .with(font:UIFont(name: "Times New Roman", size: 37)!)

关于ios - 将 HTML 转换为 NSAttributedString 时可以设置默认字体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263767/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4