gpt4 book ai didi

swift - 为什么 iOS13 更改 NSAttributedString 中段落的字体系列

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:44 24 4
gpt4 key购买 nike

我的应用程序使用 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 字体。

最佳答案

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

关于swift - 为什么 iOS13 更改 NSAttributedString 中段落的字体系列 <p>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57925735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com