我的应用程序使用NSAttributedString向用户显示文本。大部分文本都在段落HTML标记(<p>
)内。当它在这些标记内部时,字体族被更改为某个Times风格。
这是在新版本iOS 13中发生的问题,之前我们没有遇到过这个问题。
On this image you can see normal font and the modified font when it is in the
tags: https://imgur.com/a/YY3Gdph
We don't know where to start looking for the solution.
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
I expect to see standard iOS font.
font
或font.fontName
? - Larmefont.fontName
应该明确指定所需字体系列中的哪种字体,例如细体、粗体、黑体等。 - Rado