【问题标题】:Setting font in html for UILabel attributed text is not working在 html 中为 UILabel 属性文本设置字体不起作用
【发布时间】:2019-05-22 09:24:25
【问题描述】:

带有font-size 的HTML 文本或在UILabel 上设置UIFont 会使NSAttributedText 无法正确呈现。

extension String {
    var htmlToAttributedLabelString: NSAttributedString? {
        guard let data = NSString(string: self).data(using: String.Encoding.utf8.rawValue) else { return nil }
        do {
            return try NSAttributedString(data: data,
                                          options: [.documentType: NSAttributedString.DocumentType.html,
                                                    .characterEncoding: String.Encoding.utf8.rawValue],
                                          documentAttributes: nil)
        } catch {}
        return nil
    }
}
let lbl = UILabel()
lbl.attributedText = "<span style=\"font-size: 22px\"; Please read the <b>Nomination Guidelines</b> before completing this form.".htmlToAttributedLabelString

这不是粗体。如果我尝试手动将字体设置为标签,也会出现同样的问题。如何解决这个问题?

【问题讨论】:

    标签: ios swift uilabel nsattributedstring


    【解决方案1】:

    在您正在使用的html text 中,span 标记未关闭。

    尝试将lbl.attributedText 的值更改为:

    lbl.attributedText = "<span style=\"font-size: 22px\">; Please read the <b>Nomination Guidelines</b> before completing this form.".htmlToAttributedLabelString
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多