我收到日志消息 UITextView 正在切换到 TextKit 1 兼容模式,因为它的 textStorage 包含与 TextKit 2 不兼容的属性,通过使用
documentType: NSAttributedString.DocumentType.html
作为 NSAttributedString。
例子:
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
return textView
}
func updateUIView(_ uiView: UITextView, context _: Context) {
var currentText: NSAttributedString?
let htmlString = "<html><body><h1>Example</h1></body></html>"
guard let encodedData = htmlString.data(using: .utf8) else {
fatalError("Could not encode HTML data")
}
do {
currentText = try NSAttributedString(data: encodedData,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
],
documentAttributes: nil)
} catch let error as NSError {
fatalError(error.localizedDescription)
} catch {
fatalError("error")
}
uiView.attributedText = currentText
}
有什么办法解决吗?
在 iOS 16+ 中,默认情况下
UITextView
使用 TextKit 2
。如果你想使用TextKit 1
,使用这个构造函数构造UITextView
:
init(usingTextLayoutManager: Bool)
创建一个新的文本视图,有或没有文本布局管理器取决于您指定的布尔值。
如果
usingTextLayoutManager
为真,UITextView
使用TextKit 3
。如果是false
,则使用TextKit 1
。
let textView = UITextView(usingTextLayoutManager: true)
let textView = UITextView(usingTextLayoutManager: false)
NSTextView
也一样。