3
votes

I have a UITextView and I am recording all the text view change events into an array.

When the user is fast deleting (holding down the delete key so words at deleted at a time and not just one character), when the textView:shouldChangeTextInRange:replacementText method is called, the range variable's length is only 1, when in reality the range should be the number of letters deleted in the word. Does anyone know of any work-arounds to this Apple Bug so I can correctly identify the location and range and the text changed in the UITextView?

2

2 Answers

0
votes

No, its not a bug. This function will get call for each time your text is changing. Probably you are working with debug mode and giving backspace with keyboard. It will generate multiple event before hitting the breakpoint. So, the function will get call multiple times.

0
votes

As you have mentioned in your question the 'range' is always 1 even if a whole word is deleted. You have to check the length of range 'range.length' which will provide you exact count of characters being deleted from your UITextView. I had same scenario and did some work around based on it which will only allow to delete a single character at once not the whole word:

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@""]) {
        // Check if length of range being deleted is less than 1 then only delete it
        if (range.length > 1) {
            return NO;
        } else
            return YES;
    } else {
        return YES;
    }
}