For Swift 4.2, you can add UITextViewTextDidChange
, UITextViewTextDidBeginEditing
& UITextViewTextDidEndEditing
notification observers to a UITextView like this:
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(textViewDidChange), name: NSNotification.Name.UITextViewTextDidChange , object: nil)
nc.addObserver(self, selector: #selector(textViewDidBeginEditing), name: NSNotification.Name.UITextViewTextDidBeginEditing , object: nil)
nc.addObserver(self, selector: #selector(textViewDidEndEditing), name: NSNotification.Name.UITextViewTextDidEndEditing , object: nil)
And the observers look like this:
@objc func textViewDidChange(_ notif: Notification) {
guard notif.object is UITextView else { return }
// Do something
}
@objc func textViewDidBeginEditing(_ notif: Notification) {
guard notif.object is UITextView else { return }
// Do something
}
@objc func textViewDidEndEditing(_ notif: Notification) {
guard notif.object is UITextView else { return }
// Do something
}
For Swift5, the format of the 'name' values are UITextView.textDidChangeNotification
.