在用户输入时更改UILabel的文本颜色

时间:2014-11-30 23:40:49

标签: ios objective-c

我有UILabel,显示UITextfield的字数。它有效,但当x小于零时,我无法更改标签的文字颜色。实际上我不知道答案在哪里,因为我可以记录/显示正确的数据,只有-1,-2..处的颜色没有变化。

- (void)updateLabelUsingContentsOfTextField:(id)sender {

    NSString *numberPlaceholder = [NSString stringWithFormat:@"%@", ((UITextField *)sender).text];

    int x = 43-[numberPlaceholder length];

    if (x <= 0) {

        self.charNumbers.textColor = [UIColor redColor];
        NSLog(@"IF DEV LOG 1 - INTEGER IS %d", x);

    }

    self.charNumbers.text = [NSString stringWithFormat:@"%d",x];

    NSLog(@"X INTEGER IS %d", x);

    if (x <= 43) {

        self.charNumbers.textColor = [UIColor blackColor];
           NSLog(@"IF DEV LOG 2 - INTEGER IS %d", x);
    }

}

1 个答案:

答案 0 :(得分:2)

[numberPlaceholder length]返回无符号整数,因此不正确的比较可能是下溢的结果。尝试将[numberPlaceholder length]转换为int,因此值的类型相同:

int x = 43 - (int)[numberPlaceholder length];