6

我的应用程序使用 NSAttributedString 向用户显示文本。大部分文本位于段落 HTML 标记 ( <p>) 内。当它在标签内时,字体系列将更改为某些 Times 样式。
它发生在新版本的 iOS 13 中,在我们没有遇到这个问题之前。

在此图像上,您可以看到正常字体和修改后的字体

标签:https://imgur.com/a/YY3Gdph

我们不知道从哪里开始寻找解决方案。

let font = UIFont.preferredFont(forTextStyle: .body)
let strDesc: String = "\(explanation)\n"
let newStringDesc = strDesc.replacingOccurrences(of: "</ul>", with: "</ul><br>")

let modifiedString = "<style>body{font-family: '\(font.fontName)'; font-size:\(font.pointSize)px;}</style>\(strDesc)"
guard let data = modifiedString.data(using: .utf8) else { return nil }

let attrString = try NSMutableAttributedString(
    data: data,
    options: [
              NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,

NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue,
                ],
    documentAttributes: nil
)
attrString.addCustomParagraphSettings(font: font)
attrString.setCustomParagraphFontColor()

let mainAttributedText = NSMutableAttributedString()

mainAttributedText.append(attrString)
mainAttributedText.append(tmpTakenText)
descriptionTextView.attributedText = mainAttributedText

我希望看到标准的 iOS 字体。

4

1 回答 1

3

@Larme 在代码中发现了一个错误。CSS 属性的值font-family应该是font.familyName而不是font.fontName

于 2019-09-13T15:40:29.960 回答