iOS 13中的归因字符串问题

问题描述 投票:0回答:2

我正在使用以下扩展名将HTML字符串转换为属性字符串,以呈现我正在使用UILabel的属性字符串。字体家族是系统常规的,iPhone / iPad分别为15/18。在上面的图中,Description是标题,属性字符串是值。

extension NSAttributedString {

    internal convenience init?(html: String, font : UIFont) {

        let modifiedFont = String(format:"<span style=\"font-family: '-apple-system', '\(font.fontName)'; font-size: \(font.pointSize)\">%@</span>", html)

        guard let data = modifiedFont.data(using: String.Encoding.utf16, allowLossyConversion: false) else {
            return nil
        }

        guard let attributedString = try? NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
            return nil
        }

        self.init(attributedString: attributedString)
    }
}

在上述扩展名中控制台打印变量

(lldb) po html
"<p><font size=\"2\">Pack Size: 2.5 Gal x 2</font></p><p><font size=\"2\">Gallons per case / bucket / barrel:</font></p><p><font size=\"2\">1 Case – 5.0 Gal<br>&nbsp;2 Cases – 10.0 Gal<br>&nbsp;3 Cases – 15.0 Gal</font></p>"

(lldb) po font
<UICTFont: 0x7ffbe4b03a20> font-family: ".SFUI-Regular"; font-weight: normal; font-style: normal; font-size: 15.00pt


(lldb) po modifiedFont
"<span style=\"font-family: \'-apple-system\', \'.SFUI-Regular\'; font-size: 15.0\"><p><font size=\"2\">Pack Size: 2.5 Gal x 2</font></p><p><font size=\"2\">Gallons per case / bucket / barrel:</font></p><p><font size=\"2\">1 Case – 5.0 Gal<br>&nbsp;2 Cases – 10.0 Gal<br>&nbsp;3 Cases – 15.0 Gal</font></p></span>"

我正在像下面那样拨打分机号

self.lblValueDescription.attributedText = NSAttributedString(html: data.descriptionField,
                                                                     font: self.lblValueDescription.font)

iOS 12中的输出

enter image description here

iOS 13中的输出

enter image description here

问题:

  1. 字体大小与iOS12中的预期大小不同
  2. iOS13的某些外来字符具有相同的html字符串。
ios swift nsattributedstring
2个回答
1
投票
extension String { func htmlAttributed(family: String?, size: CGFloat, color: UIColor) -> NSAttributedString? { do { let htmlCSSString = "<style>" + "html *" + "{" + "font-size: \(size)pt !important;" + "color: #000000 !important;" + "font-family: \(family ?? "Helvetica"), Helvetica !important;" + "}</style> \(self)" guard let data = htmlCSSString.data(using: String.Encoding.utf8) else { return nil } return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print("error: ", error) return nil } } }

并在此行设置文本

 self.lblValueDescription.attributedText = yourHtmlString.htmlAttributed(family: "Roboto-Regular", size: 11, color: UIColor.black)!

编辑:

查看完答案后,我做了以下更改

let modifiedFont = String(format:"<style>html *{font-family: '-apple-system', '\(font.fontName)' !important; font-size: \(font.pointSize) !important}</style>%@", html)

用样式块修改样式标签
    [!important标志添加为强制应用样式。

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.