I have a UITextView
configured with a delegate. I've enabled editing attributes by setting allowsEditingTextAttributes
to YES
.
When the user types a character in the text view, the delegate receives the textViewDidChange
: message.
But when the user changes an attribute (such as making a selection and tapping Bold or Italic), no textViewDidChange
: message.
The documentation specifically says that textViewDidChange
: should receive a message when the user changes attributes:
Tells the delegate that the text or attributes in the specified text view were changed by the user.
But it's not working for me. What am I missing here?
I tested this scenario in iOS 6 and had the exact same outcome: attribute changes from the "select" pop-up did not trigger the textViewDidChange:
method. It seems that this is a bug or the documentation needs to clarify what type of attribute change would trigger this event.
A possible workaround is to implement the textViewDidChangeSelection:
method. It gets called whenever a selection is made (which the user would have to do before changing an attribute). Check to see if the selectedRange.length is > 0 (which would mean an actual word has been selected, instead of just moving the cursor around), and save that selectedRange. Once the length is zero again, it means they deselected the item. At that time, you could take the previous range and work with the text.
- (void)textViewDidChangeSelection:(UITextView *)textView
{
static BOOL rangeSet = NO;
static NSRange mySelectedRange;
if( textView.selectedRange.length > 0 && !rangeSet )
{
mySelectedRange = textView.selectedRange;
rangeSet = YES;
}
else if( textView.selectedRange.length == 0 && rangeSet)
{
// Work with text
NSLog(@"Working with previously select text: %d, %d", mySelectedRange.location, mySelectedRange.length);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With