Leon
Leon

Reputation: 417

count characters in a textview

i want to count the characters in a textview which are typed in by the user. I gave it some thoughts and my ideas were that I have to create a NSTimer with a selector which checks the length. So I've got:

-(void)viewDidLoad { [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkText) userInfo:nil repeats:YES];  }

-(void)checkText {

int characters;

label.text.length = self.characters;

characterLabel.text = [NSString stringWithFormat:@"%i", characters]; }

This doesn't work because "Request for member "characters" in something not a structure or union"

How can I solve this problem?

Upvotes: 0

Views: 3139

Answers (2)

Ziggy
Ziggy

Reputation: 227

Please note that if you are trying to use self.characters to get int characters variable's value - it won't work. self.characters code means you have a property setup @property characters and it will call this property's getter. To use local variable just use its name.

I think you have mixed-up label objects in your -checkText: method. Here's a generic example of how to get UITextView's number of characters:

int characters = [myTextView.text length];

Upvotes: 0

Bartosz Ciechanowski
Bartosz Ciechanowski

Reputation: 10333

There is no need for NSTimer. Setup yourself as textview's delegate and implement:

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

    characterLabel.text = [NSString stringWithFormat:@"%u", length]
}

Upvotes: 5

Related Questions