How can I get the length of text entered into UITextView as the user types?

94 views Asked by At

I have aUITextView. I would like to have a way of updating aUILabel as the user times to indicate how many remaining characters are permitted?

In theViewController theUITextView is referenced as IBOutlet UITextView *message;

I have tried looking at the show connections inspector and I don't see an edit changed option.

I looked at this example UITextField text change event

But it seemed to apply toUITextField notUITextView

1

There are 1 answers

5
App Dev Guy On BEST ANSWER

This should help:

- (void)textViewDidChange:(UITextView *)textView{

    NSString *stringCount = self.textView.text;
    int count = (int) stringCount.length;
    //assumes you have a label, see the attached gif
    self.label.text = [NSString stringWithFormat:@"TextView count is: %d",count];

    NSString *stringcount222 = self.textview222.text;
    int count222 = (int) stringcount222.length;
    self.label222.text = [NSString stringWithFormat:@"No. 2 count is: %d", count222]; 

}

You need to select the TextView that you are using and control+drag to the ViewController icon and select Delegate:

enter image description here

The you should be able to get a result like this:

enter image description here