2

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?

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195

2 Answers2

5

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

Dhruv Goel
  • 2,715
  • 1
  • 17
  • 17
  • Very nice! Thankyou so much! I've never actually used a break in a for loop before so at first I was confused as to why this code worked and didn't delete all e's in the string but now I see! – Albert Renshaw Feb 05 '13 at 07:30
1

Kind of fun, so I wrote the code just now. The code works.

First, we should set the UITextView's delegate and respond to .

textView:shouldChangeTextInRange:replacementText:

According to the document,

If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character.

So the code comes below :

#pragma mark - 
#pragma mark - UITextView Delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    static NSString *suffix = @"e";

    if (range.length == 1 && [text length] == 0) {
        // The user presses the Delete key.
        NSString *currentText = [textView.text substringToIndex:range.location+1];
        NSString *appendingText = [textView.text substringFromIndex:range.location+1];

        if ([currentText hasSuffix:suffix]) {
            NSRange range = [self inverseRangeOfString:currentText withSuffix:suffix];
            currentText = [currentText stringByReplacingCharactersInRange:range withString:@""];

            textView.text = [currentText stringByAppendingString:appendingText];

            return NO;
        }
    }

    return YES;
}

- (NSRange)inverseRangeOfString:(NSString *)str withSuffix:(NSString *)suffix
{
    int length = [str length];
    int lastIndex = length - 1;

    int cnt = 0;
    for (; lastIndex >= 0; --lastIndex) {
        NSString *subStr = [str substringFromIndex:lastIndex];
        if ([subStr hasPrefix:suffix]) {
            cnt++;
        } else {
            break;
        }
    }

    NSRange range = (NSRange){++lastIndex, cnt};

    return range;
}
Jason Lee
  • 3,200
  • 1
  • 34
  • 71