Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond to programmatic changes of a TextView text

The TextView delegate is set:

textView.delegate = self //self being a UITextViewDelegate

but the delegate method doesn't get called when the text is set programmatically

func textViewDidChange(_ textView: UITextView) {
    print(textView.text)
}

How to respond to text changes without going reactive?

like image 990
ielyamani Avatar asked Jan 21 '26 01:01

ielyamani


1 Answers

I just tried KVO on UITextView,

 self.textView1.text = "You are working, but I will change you in 5 seconds"

Add your observer

self.textView1.addObserver(self, forKeyPath: "text", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)

Trigger text change programmatically, just an example do it the way you want.

 DispatchQueue.main.asyncAfter(deadline:.now() + 5) {
    self.textView1.text = "Changed after some time"
}

Override the KVO method.

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if object == self.textView1{
     //do your thing here...
    }
}

FYI from Apple docs below

Note: Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views.

https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html

like image 147
Satheesh Avatar answered Jan 22 '26 14:01

Satheesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!