SiwiftUI:UITextView 正在切换到 TextKit 1

问题描述 投票:0回答:1

我收到日志消息 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
}

有什么办法解决吗?

swiftui uitextview ios16 textkit
1个回答
0
投票

您可以选择 TextKit 1 或 TextKit 2.

在 iOS 16+ 中,默认情况下

UITextView
使用
TextKit 2
。如果你想使用
TextKit 1
,使用这个构造函数构造
UITextView

init(usingTextLayoutManager: Bool)

创建一个新的文本视图,有或没有文本布局管理器取决于您指定的布尔值。

https://developer.apple.com/documentation/uikit/uitextview

如果

usingTextLayoutManager
为真,
UITextView
使用
TextKit 3
。如果是
false
,则使用
TextKit 1

文本工具包 2

let textView = UITextView(usingTextLayoutManager: true)

文本工具包 1

let textView = UITextView(usingTextLayoutManager: false)

NSTextView
也一样。

© www.soinside.com 2019 - 2024. All rights reserved.