Detecting selection change on a UITextView?
ios, objective-c, uitextview
Solution
`UITextView` adopts the `UITextInput` protocol. That protocol includes a required `selectedTextRange` read/write property. That is the property you want to respond to changes of.
While you could observe that property, the simplest approach is to override the `-setSelectedTextRange:` method in your subclass and perform whatever additional behaviours you need in there. (You may or may not want to confirm that the range is actually changing, depending on your particular design.)
Problem
Is there a way to detect when the user changes the range of the selected text in a textview (In a subclass of UITextView)? I couldn't find any NSNotification. I tried observing keyvalues but the observer for selected range does not get called ``` [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 delegate method of UITextView