views:

587

answers:

1

Hello All ,

I am giving a text view to tweet some string .

I am applying the following method to restrict the number of characters to 140 in length.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{


    if([text isEqualToString:@"\b"]){
        DLog(@"Ohoooo");
        return YES;
    }else if([[textView text] length] > 140){

        return NO;
    }

    return YES;
}

The code is working nicely except the first condition that backspace is not working. suppose that I have reached the limit of 140 characters so that the method will give me false and the user can not insert more characters but after that when I try to delete some characters the text view behave as it is disabled .

So the question how do delete characters from textview.text or re-enable the text view .

+5  A: 

You should be looking for an empty string instead, as the apple reference says

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

David Gelhar
thanks but can be more specific I can not understand the technical specification of apple
hib
just tell me what should I put in the first condition . I am gooling for this for almost 4 hours
hib
ok got it like range.length ==1 it work fine
hib
I think the check you actually want to make is something like `[[textView text] length] - range.length + text.length > 140`, to account for cut/paste operations.
David Gelhar
good thinking . keep it up the good work .
hib