【问题标题】:Is there a way to change the color of the text inside a textfield as you are typing?有没有办法在您输入时更改文本字段内文本的颜色?
【发布时间】:2014-04-08 20:21:49
【问题描述】:

如果文本字段的文本长度小于 4,我希望更改字体的颜色。但我只希望它在我完成输入后显示(离开文本字段)。

我有这个

if ([_username.text length] < 4){
    _username.textColor = [UIColor redColor];
}
else{
    _username.textColor = [UIColor blackColor];
}

但我不知道我必须“把它放在里面”(目标 C 的新手 - 不懂行话)。

我已经尝试过了,但它不能正常工作,因为当您返回文本字段时,字体会保持红色。我想要它,这样当你打字时它总是黑色的。而且只有当你按下返回(下一步)键时它才会变成红色。

- (BOOL)textFieldShouldReturn:(UITextField *)textField 

还有为什么这不起作用?

if (textField == _confirm) {
        int resultantLength = textField.text.length + string.length - range.length;
        
        NSLog(@"Length After Additions And Subtractions Will Be: %i letters", resultantLength);
        
        if (![_confirm.text isEqualToString:_password.text]) {
            textField.textColor = [UIColor redColor];
            [_next setEnabled:NO];

        }else{
            [_next setEnabled:YES];
        }
    }

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    如果您想等到用户完成,请添加:

    - (void)textFieldDidEndEditing:(UITextField *)textField {
        if (textField.text.length < 4){
            textField.textColor = [UIColor redColor];
        }
        else{
            textField.textColor = [UIColor blackColor];
        }
    }
    

    如果您想实时进行:

    - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        int resultantLength = textField.text.length + string.length - range.length;
    
        NSLog(@"Length After Additions And Subtractions Will Be: %i letters", resultantLength);
    
        if (resultantLength < 4){
            textField.textColor = [UIColor redColor];
        }
        else{
            textField.textColor = [UIColor blackColor];
        }
    
        return YES;
    }
    

    请注意,这将影响使用相同委托的任何文本字段。如果您只想回复特定的文本字段,例如 _username,请执行以下操作:

    - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        if (textField == _username) {
            int resultantLength = textField.text.length + string.length - range.length;
    
            NSLog(@"Length After Additions And Subtractions Will Be: %i letters", resultantLength);
    
            if (resultantLength < 4){
                textField.textColor = [UIColor redColor];
            }
            else{
                textField.textColor = [UIColor blackColor];
            }
        }
        return YES;
    }
    

    确保您的 ViewController 已针对 UITextFieldDelegate 协议进行设置,并且它是您的 textField 的委托

    【讨论】:

    • 请重新阅读问题。 OP 希望在用户离开文本字段后更改颜色,而不是更改颜色。
    • @Logan 我发现了这个问题。如果您首先输入“你好”一直到“地狱”是红色的,然后当您添加“o”时它是黑色的 - 很好。但是如果你去掉“o”它仍然是黑色的。它应该变回红色吗?
    • @Maximilian - 我刚刚注意到并修复了它。
    • @Logan 虽然这么说,但我认为实时显示它可能是更好的用户体验。
    • 我发现一个新问题是我的错。我忘了告诉你我还有另外两个文本字段。编辑时似乎会改变第一个的颜色。
    【解决方案2】:

    要在每次按键时更改文本颜色,请执行以下操作:

    [myTextField addTarget:self action:@selector(textFieldDidChange:)
                      forControlEvents:UIControlEventEditingChanged];
    
    //....
    
    - (void)textFieldDidChange:(UITextField *)textField
    {
        if (textField.text.length > 4) {
          //change color
        } else {
           //change to another color
        }
    }
    

    【讨论】:

    • 或者干脆使用textFieldDidEndEditing:委托方法。
    【解决方案3】:

    您必须使用NSAttributedString。 浏览单词并找到您的字符或字母的范围并更改其颜色。

    - (IBAction)colorWord:(id)sender {
    NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.text.text];
    
    NSArray *words=[self.text.text componentsSeparatedByString:@" "];
    
    for (NSString *word in words) {        
        if ([word hasPrefix:@"@"]) {
            NSRange range=[self.text.text rangeOfString:word];
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];           
        }
    }
    [self.text setAttributedText:string];
    

    }

    上面的代码将用于更改句子中特定单词的颜色。我相信你可以对一个单词中的一个字符做同样的事情。

    祝你好运!

    ERID:这里是我获取代码的地方。还有一个工作应用程序,所以你可以看看。 AttributedStringOneWord

    【讨论】:

    • 这不是 OP 需要的。只需设置文本字段的textColor 即可。
    • 嗯,是的,对不起,我的印象是他想改变每个角色的颜色。不是整个词。很好的收获。
    【解决方案4】:

    您可以实现UITextFieldDelegate 方法:

    1. -textFieldDidEndEditing:
      • 根据需要设置红色
    2. -textFieldShouldBeginEditing:
      • 重置为黑色(以防文本为红色而您返回编辑)

    例子:

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        //to set font color to black when user begins editing the textField
        [textField setTextColor:[UIColor blackColor]];
    
        return YES;
    }
    
    -(void)textFieldDidEndEditing:(UITextField *)textField
    {
        //for implementing the red font color logic
        NSString *strCurrent = textField.text;
    
        if (strCurrent.length < 4) {
            [textField setTextColor:[UIColor redColor]];
        } else {
            [textField setTextColor:[UIColor blackColor]];
        }
    }
    

    既然你是新手...
    注意:

    1. textField 对象设置委托
      • [textFieldObject setDelegate:self];
    2. 在类文件的.h 中,声明协议&lt;UITextFieldDelegate&gt;
      • 示例:@interface ViewController : UIViewController &lt;UITextFieldDelegate&gt;

    【讨论】:

      猜你喜欢
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多