有没有办法在输入时更改文本字段内的文本颜色?

时间:2014-04-08 20:21:49

标签: ios objective-c uitextfield

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

我有这个

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

但我不知道我有什么要把它放在&#34; (对于客观的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];
        }
    }

4 个答案:

答案 0 :(得分:2)

如果您想等到用户完成,请添加以下内容:

- (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;
}

请注意,这将影响使用相同委托的任何textField。如果您只想回复特定的textField,例如_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的委托

答案 1 :(得分:1)

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

[myTextField addTarget:self action:@selector(textFieldDidChange:)
                  forControlEvents:UIControlEventEditingChanged];

//....

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField.text.length > 4) {
      //change color
    } else {
       //change to another color
    }
}

答案 2 :(得分:0)

您必须使用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

答案 3 :(得分:0)

您可以实施UITextFieldDelegate方法:

  1. -textFieldDidEndEditing:
      如果需要,
    • 设置红色
  2. -textFieldShouldBeginEditing:
    • 重置为黑色(如果文字为红色,你又回来编辑)
  3. 示例:

    -(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中,声明协议<UITextFieldDelegate>
      • 示例:@interface ViewController : UIViewController <UITextFieldDelegate>