【问题标题】:Changing background color of textfield depending on value entered [duplicate]根据输入的值更改文本字段的背景颜色[重复]
【发布时间】:2013-12-07 00:24:18
【问题描述】:

美好的一天!我只想问是否可以根据输入的值更改 UITextField 的背景颜色?或者我可以设置任何特定颜色的值吗? 例如我设置5,10,15,20红色和3,6,9,12绿色?我试过这段代码,但只针对单一颜色,我想根据我输入的值更改为,

 case 0:
        yy = self.txtTimestamp.frame.origin.y + self.txtTime.frame.size.height;
        self.txtTimestamp.backgroundColor = [UIColor blackColor];
        break;
case 1:
        yy = self.txtTrucknumber.frame.origin.y + self.txtDrivername.frame.size.height;
        //self.txtDrivername.backgroundColor = [UIColor blackColor];
        break;
case 2:
        yy = self.txtDrivername.frame.origin.y + self.txtClockin.frame.size.height;
        //self.txtClockin.backgroundColor = [UIColor blackColor];
        break;
case 3:
        yy = self.txtSsn.frame.origin.y - self.txtTrucknumber.frame.size.height;
        //self.txtTrucknumber.backgroundColor = [UIColor blackColor];
        break;
case 4:
        yy = self.txtClockin.frame.origin.y - self.txtTrucknumber.frame.size.height;
        //self.txtTrucknumber.backgroundColor = [UIColor blackColor];
        break;

【问题讨论】:

  • 是的,你可以做到。但您需要先存储所有值,然后才使用输入值的条件。
  • 你能提供一个好的样品吗?谢谢!

标签: ios uitextfield


【解决方案1】:

如果你介意(如果它尊重你的要求),你可以用 UITextView 替换你的 UITextField,并使用委托方法:

- (void)textViewDidChange:(UITextView *)textView

因此您可以根据textView.text 的属性更改textView 的背景颜色。
对于自定义颜色,请使用:

[UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>]

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2011-11-28
    • 1970-01-01
    • 2011-11-05
    • 2018-03-24
    • 1970-01-01
    • 2019-01-08
    • 2021-07-05
    相关资源
    最近更新 更多