gpt4 book ai didi

swiftui - SwiftUI UITextView协调器不起作用

转载 作者:行者123 更新时间:2023-12-03 09:49:37 29 4
gpt4 key购买 nike

我已经将UITextView包装在UIViewRepresentable中,并包括了Coordinator作为UITextViewDelegate,但是未调用事件。我究竟做错了什么?


struct TextView : UIViewRepresentable {
typealias UIViewType = UITextView

var placeholder : String

@Binding var text: String

func makeCoordinator() -> Coordinator {
Coordinator(self)
}

func makeUIView(context: UIViewRepresentableContext<TextView>) -> UITextView {
let textView = UITextView()
let placeholderLabel = UILabel()
textView.font = UIFont(name: "Helvetica", size: 16)
placeholderLabel.restorationIdentifier = "Placeholder"
placeholderLabel.text = self.placeholder
placeholderLabel.font = UIFont.italicSystemFont(ofSize: (textView.font?.pointSize)!)
placeholderLabel.sizeToFit()
textView.addSubview(placeholderLabel)
placeholderLabel.frame.origin = CGPoint(x: 25, y: (textView.font?.pointSize)! / 2 + 12)
placeholderLabel.textColor = UIColor.lightGray
placeholderLabel.isHidden = !textView.text.isEmpty

return textView
}

func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<TextView>) {
uiView.text = self.text
}

class Coordinator : NSObject, UITextViewDelegate {

var parent: TextView

init(_ uiTextView: TextView) {
self.parent = uiTextView
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
print("Not working")
return true
}

func textViewDidChange(_ textView: UITextView) {
print("Not working")
}


}

}


最佳答案

您必须在textView.delegate = context.coordinator中添加makeUIView才能注册协调器,以从UITextView接收更新。

关于swiftui - SwiftUI UITextView协调器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597060/

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