What is the best way to give the below UIViewRepresentable isEditing and onCommit properties? I'd like it to have the same exact functionality on call of TextFields in SwiftUI (where you can add code for what to do when the return key is pressed [onCommit] or when the textfield is clicked [isEditing])
struct TextView: UIViewRepresentable {
@Binding var text: String
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> UITextView {
let myTextView = UITextView()
myTextView.delegate = context.coordinator
myTextView.font = UIFont(name: "HelveticaNeue", size: 16)
myTextView.isScrollEnabled = true
myTextView.isEditable = true
myTextView.isUserInteractionEnabled = true
myTextView.backgroundColor = UIColor(white: 0.0, alpha: 0.00)
myTextView.textColor = UIColor.black
return myTextView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = 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 {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}
func textViewDidChange(_ textView: UITextView) {
print("text now: \(String(describing: textView.text!))")
self.parent.text = textView.text
}
}
}
Side Note: I'm not sure why, but the binding variable that I am passing in is not changing the actual value. I know because I set up a custom binding with get/set properties and the set is not printing the value. (this value should be the text passed into the textview)
let binding = Binding<String>(get: {
self.bindingVariableName
}, set: {
print("set the value")
self.bindingVariableName = $0
}
)