-4

I have five textview in same page. But just one textview editable. i wanna limit this textview characters. I use - (void)textViewDidChange:(UITextView *)textView but it didn't work. My UITextView reference name is UserNote. I try - (void)textViewDidChange:(UITextView *)UserNote Unfortunately did not work again.
Please help me. Thanks. I Change My reference Name it is textView now.

My Code ;

@property (retain, nonatomic) IBOutlet UITextView *textView;

- (void)textViewDidChange:(UITextView *)textView{
    NSUInteger length;
    length = [textView.text length];

    NSString * last = [NSString stringWithFormat:@"%lu", 160 - length];

    [_characterLabel setText:[NSString stringWithFormat:@"%@",last]];

    NSLog(@"%@" , last);
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    // Prevent crashing undo bug – see note below.
    if(range.length + range.location > textView.text.length)
    {
        return NO;
    }

    NSUInteger newLength = [textView.text length] + [text length] - range.length;
    return (newLength > 160) ? NO : YES;
}
Chris
  • 7,270
  • 19
  • 66
  • 110
  • did you set the delegate to your textview ? – tcacciatore Dec 19 '14 at 14:51
  • possible duplicate of [Limit number of characters in uitextview](http://stackoverflow.com/questions/2492247/limit-number-of-characters-in-uitextview) – Gaëtan Maisse Dec 19 '14 at 15:02
  • you can set a tag to each textView and in delegate method you can write: if(textView.tag == 0) //0 is an example you should put the number of tag of editable textview... – Ilario Dec 19 '14 at 15:12
  • Show the code you used and tell us what "did not work" means. (That is, what happened that was different from what you wanted.) – Phillip Mills Dec 19 '14 at 15:25
  • i try with tag. Didn't work. Thanks. – Hakan OZGUR Dec 19 '14 at 16:04
  • When you set a breakpoint in these methods, does it hit the breakpoint? If not, set your textview's delegate. What exactly isn't working? – Chris Dec 19 '14 at 19:12
  • My project is working. Just - (void)textViewDidChange:(UITextView *)textView methods didnt work. I use NSLog in this method and edit textView but i didnt see my log message. – Hakan OZGUR Dec 19 '14 at 19:43

2 Answers2

0

i solved it. Just i forgot setup UITextView Delegate. Thanks.

0

This expression it's good

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

NSString *string = [[textView text] stringByReplacingCharactersInRange:range withString:text];

if ([string length] > 60)
{
    return NO;
}
return YES;
}
NSViking
  • 53
  • 6