blog
blog copied to clipboard
How to use UITextView in SwiftUI
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
}
}
Hey man, I'm using pretty much the same pattern for my TextView. There is one big problem that isn't visible in first place. When you wrote some text and then you want to edit in the middle of the text the cursor jumps to the end of the text after every typed character. The culprit in this case is textViewDidChange
. Do you have a solution for that?