user3909859
user3909859

Reputation: 103

TextView text not editing

I have a limit of 140 words on my UITextView. When I type 140 words it works fine. Now I move to next controller with this text and then back to previous controller with this text. I'm setting this text to my UITextView but this is not editable. If my length is less than 140 words it works fine. This is my code

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)texst {
    [myScrollView setContentOffset:CGPointMake(0, 100)];

    if([texst isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        [myScrollView setContentOffset:CGPointMake(0, 0)];
        return NO;
    }

    return textView.text.length  < 140;
}

Upvotes: 1

Views: 61

Answers (1)

Elliot Alderson
Elliot Alderson

Reputation: 546

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    [myScrollView setContentOffset:CGPointMake(0, 100)];

    if([texst isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        [myScrollView setContentOffset:CGPointMake(0, 0)];
        return NO;
    }
    if(range.length + range.location > textView.text.length)
        return NO;

    NSUInteger newLength = [textView.text length] + [text length] - range.length;
    return (newLength >= 140) ? NO : YES;
}

Just copy and paste this. I didn't even bother trying to figure out how it works, but I copied it from another stackoverflow question and I've been using it since. Apparently it even works with the "shake to undo" feature.

By the way, it looks like your last line of code is the main problem. If the text length is 140 then the method will return false and you wont be able to edit the text (even if you backspace (it will still be 140 and return false)). Anyways, good luck.

Upvotes: 1

Related Questions