开发者

Weird UITextView behavior

开发者 https://www.devze.com 2023-04-12 19:42 出处:网络
I have a UITextView which i create in the code: myView = [[UITextView alloc] initWithFrame:CGRectMake(10,5,220,50)];

I have a UITextView which i create in the code:

myView = [[UITextView alloc] initWithFrame:CGRectMake(10,5,220,50)];
myView.editable = YES;
myView.font = [UIFont fontWithName:@"Helvetica" size:16];
myView.bounces = NO;
myView.delegate = self;

I set UIView *myView in the header as well.. and also set delegate - UIViewController UITextViewDelegate>

I want to hide a label every time the user writes a text for that i check if its equal to 0, else its hide.

the problem is in this method -

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if(textView.text.length == 0)
        descLabel.hidden = NO;
    else 
        descLabel.hidden = YES;

    return YES;
}

the program runs but when i enter the first letter the label still there, and when i enter the second letter it goes as it should. then i delete those two and its still gone! (reminder: i want the label to be hidden when the length is 0) but when i press on the delete again i can see the label. Pretty weird..

I want开发者_运维百科 to have the behavior of a placeholder in UITextView but this problem annoying

Thanks for your help!


You are checking the textView.text value before the change; you presumably want to check the length after the update would be applied.

e.g.:

NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
0

精彩评论

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