I want to check if a user backspaces a character in a textView if there are any of that same character connecting it for it to delete them all...
For example if the character I'm checking for is "e" I have the text "easy heeeeeello" and the user starts hitting backspace it will become:
easy heeeeeello -> easy heeeeeell -> easy heeeeeel -> easy heeeeee -> easy h
The code should detect that a backspace was pressed. Then it will detect which text is going to be deleted, and if that text is a character (in our case "e") it will check if there are more "e"s touching that "e" creating a strand of "e"s and delete them all.
Can you help me?
OK, so I wrote this code, and it works for me
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@""]) {
//Detected backspace character as the new character is @"" meaning something will be deleted
char toDelete = [textView.text characterAtIndex:range.location];
int duplicateCharCount = 0;
for(int i =range.location-1; i>=0; i--) {
if([textView.text characterAtIndex:i] == toDelete) {
duplicateCharCount++;
} else {
break;
}
}
NSRange newRange = NSMakeRange(0, range.location - duplicateCharCount);
[textView setText:[textView.text substringWithRange:newRange]];
return NO;
} else {
return YES;
}
}
I know its not the best implementation, but now you know how to proceed
Hope this helps