我的应用程序使用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字体。
答案 0 :(得分:1)
@Larme在此代码中发现了一个错误。 font-family:应该按font.familyName