Detecting selection changes in a UITextView?

Is there a way to detect when the user changes the range of the selected text in text form (in a subclass of UITextView)? I could not find NSNotification. I tried to observe key values, but the observer for the selected range did not receive a call

[self addObserver:self forKeyPath:@"selectedRange" options:NSKeyValueObservingOptionNew context:nil]; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqual:@"selectedRange"]) { // Do stuff } } 

I DO NOT want to use the UITextView delegate method

+7
ios objective-c uitextview
source share
1 answer

UITextView accepts the UITextInput protocol. This protocol includes the required selectedTextRange read / write property. This is the property you want to respond to changes.

While you can observe this property, the easiest way is to override the -setSelectedTextRange: method in your subclass and perform any additional actions that you need. (You may or may not want to confirm that the range is really changing, depending on your specific design.)

+9
source share

All Articles