Issue #747
Need to use Coordinator
conforming to UITextViewDelegate
to apply changes back to Binding
import SwiftUI
import UIKit
struct MyTextView: UIViewRepresentable {
@Binding
var text: String
final class Coordinator: NSObject, UITextViewDelegate {
let parent: MyTextView
init(parent: MyTextView) {
self.parent = parent
}
func textViewDidChange(_ textView: UITextView) {
if textView.text != parent.text {
parent.text = textView.text
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
func makeUIView(context: Context) -> UITextView {
let view = UITextView()
view.isScrollEnabled = true
view.isEditable = true
view.isUserInteractionEnabled = true
view.font = UIFont.preferredFont(forTextStyle: .body)
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
}