Thomas
Thomas

Reputation: 36

Secure UITextField textColor

I have several UITextFields for registering a new account: name, password, etc. The password text field is Secure in the storyboard. The server returns validation information, such as if the password is too short, etc.

When a validation error is returned, I change the textColor of the appropriate textField to red to highlight that it has an error.

[messages enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSString *validationObj = (NSString*)key;
    NSArray *validationMsg = (NSArray*)obj;

    if ([validationObj isEqualToString:@"password"])
        self.registerFirstNameTextField.textColor = [UIColor redColor];

When they start editing the text field, I change color back to black using an Action connected to the "EditingChanged" event of the TextField.

- (IBAction)textFieldDidChange:(id)sender {
    UITextField *textField = (UITextField*)sender;
    textField.textColor = [UIColor blackColor];
}

In Simulator (I have not tried this on a device), this works fine for all textfields, except for the Password text field. Specifically, when I edit the password textfield, it changes to black, but as soon as the textfield loses focus, the security dots are red. Any ideas why this is and how to fix it?

Upvotes: 1

Views: 2964

Answers (1)

LeGabriel
LeGabriel

Reputation: 11

I think this is a bug. I had the exact same problem and solved it by forcing the textfield to the desired color when editing ended.

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == passwordTextField) {
        [textField setTextColor:defaultTextColour];
}

Hope this helps!

Upvotes: 1

Related Questions