Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView observe text changes

I want to listen for every text change in UITextView. The setup is very trivial.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(textViewDidChangeWithNotification(_:)),
        name: UITextView.textDidChangeNotification,
        object: nil
    )
}

@objc private func textViewDidChangeWithNotification(_ notification: Notification) {
     print("Text: \(String(describing: inputTextView.text))")
}

It works OK in most cases, but then I have found some UITextInput's black box magic.

Step 1: 'I' typed. We can see 'I' in the output.
Step 2: Important step. Select all text with double tap on the field.
Step 3: Select 'If' from word suggestions.

enter image description here

And there is no 'If' in the debuggers output. On the other side if the caret will be at the end of the 'I' word and we select 'If' output results are correct.

enter image description here

Is there any way to observe ALL text changes?

I can get text by using:

func textViewDidEndEditing(_ textView: UITextView)

but I need to observe all changes in real time. The other option I always see is to use:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool

but this method is a bad practice to observe changes. If you type two spaces repeatedly iOS will replace first space with dot and obviously will not inform you about this action in this method and a lot of other problems with it.

like image 247
evilgeniuz Avatar asked Jul 25 '26 20:07

evilgeniuz


2 Answers

OK, after a lot of research I've tried RxSwift because I thought that observing text in reactive paradigm framework should succeed in 100% cases. And it worked without any issues!

inputTextView.rx.text.subscribe(onNext: { string in
    print("rx: \(string)")
})

So it seems that these guys have found the solution.

https://github.com/ReactiveX/RxSwift/blob/master/RxCocoa/iOS/UITextView%2BRx.swift

And here is the solution that gives you information about all text changes despite of auto correction, text selection, and etc..

class ViewController: UIViewController {

    @IBOutlet weak var inputTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        inputTextView.textStorage.delegate = self
    }
}

extension ViewController: NSTextStorageDelegate {
    func textStorage(_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorage.EditActions, range editedRange: NSRange, changeInLength delta: Int) {
        print("string: \(textStorage.string)")
    }
}
like image 92
evilgeniuz Avatar answered Jul 28 '26 08:07

evilgeniuz


You can use func textViewDidChangeSelection(_ textView: UITextView) to detect the change in selection ?

like image 33
King.lbt Avatar answered Jul 28 '26 08:07

King.lbt



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!