Pregunta

I am looking to change the colour of the font if the text length of the textfield is less than 4. But I only want it to show up when I have finished typing (Gone off the text field).

I have this

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

But I don't know what I have to "put it inside of" (New to objective C - Don't know the lingo).

I have tried this but it doesn't work properly as when you return to the text field the font stays red. I want it so that when you are typing it is allways black. And also it only becomes red if you hit the return (Next) key.

- (BOOL)textFieldShouldReturn:(UITextField *)textField 

Also why is this not working?

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];
        }
    }
¿Fue útil?

Solución

If you'd like to wait til the user is done add this:

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

If you'd like to do it in real time:

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

Note that this will affect any textField that uses the same delegate. If you'd like to only respond to a specific textField, for example _username, do this:

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

Make sure your ViewController is set up for UITextFieldDelegate protocol and it is the delegate of your textField

Otros consejos

To change the text color on every key press, do this:

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

//....

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

You have to use NSAttributedString. Scan through the word and find the range of your character or letter and change its color.

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

}

The code above will work for changing the color of a specific word in a sentence. I'm sure you can do the same for a character in a word.

Good luck!

ERID: Here's where I got the code snip-it from. There's also a working app, so you can have a look. AttributedStringOneWord

You can implement the UITextFieldDelegate methods:

  1. -textFieldDidEndEditing:
    • to set red color if needed
  2. -textFieldShouldBeginEditing:
    • to reset to black color (incase the text was red and you came back to edit)

Example:

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

Since you're new...
Note:

  1. Set delegate for the textField object
    • [textFieldObject setDelegate:self];
  2. In the .h of your class file, declare the protocol <UITextFieldDelegate>
    • example: @interface ViewController : UIViewController <UITextFieldDelegate>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top