Hi there.
I want to listen for every text change in UITextView. The setup is very trivial.
It works OK in most cases, but then I have found some UITextInput's black box magic.
Step 1: Type 'I'.
Step 2: Important step. Select all text with double tap on the field.
Step 3: Select 'If' from word suggestions.
And there will be no notification for that new 'If'. This is important part for the task I have. On the other side if the caret will be at the end of the previously typed 'I' and we select 'If' then I receive notification about that change. So the difference is only in tapping 'If' suggestion when word is fully selected or not selected.
Is there any way to observe ALL text changes?
Of course I know that I can get text by using:
but I need to observe all changes in real time as user types. The other option I always see is to use:
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.
So is there any way to observe ALL text changes?
I want to listen for every text change in UITextView. The setup is very trivial.
Code Block 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: Type 'I'.
Step 2: Important step. Select all text with double tap on the field.
Step 3: Select 'If' from word suggestions.
And there will be no notification for that new 'If'. This is important part for the task I have. On the other side if the caret will be at the end of the previously typed 'I' and we select 'If' then I receive notification about that change. So the difference is only in tapping 'If' suggestion when word is fully selected or not selected.
Is there any way to observe ALL text changes?
Of course I know that I can get text by using:
Code Block func textViewDidEndEditing(_ textView: UITextView)
but I need to observe all changes in real time as user types. The other option I always see is to use:
Code Block 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.
So is there any way to observe ALL text changes?
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!
Code Block 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..
Code Block 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)") } }