开发者

count characters in a textview

开发者 https://www.devze.com 2023-02-28 22:50 出处:网络
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.

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 prob开发者_JS百科lem?


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]
}


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号