Is there any way to observe changes to the selectedTextRange of a UITextField?
I tried observing all UIControlEvents. But changing the selectedTextRange does not trigger a UIControlEvent.
Another dead end -- UIKit classes are not KVO compliant.
Then there is the UITextFieldTextDidChangeNotification. But that's for changes to the text.
Any ideas?
Subclass
UITextField
as follows.Implementation:
Then add
-textFieldDidChangeSelection:
to your text field's delegate.Caveat: This delegate message will be sent only when the cursor is moved, it will not be sent when typing or pasting text, for those events you have to implement
textField:shouldChangeCharactersInRange:replacementString:
, where the selection range is going to either be set torange.location + [string length]
(if you returnYES
) or remain the same (if you returnNO
).