Ossir
Ossir

Reputation: 3145

NSAttributedString background colour for UITextField with Secure option

I want to have red background for login/password fields when user specifies them incorrectly.

NSAttributedString *loginString = [[NSAttributedString alloc] initWithString:_loginField.text attributes:@{NSBackgroundColorAttributeName : [UIColor redColor]}];

This code works great for login, but for UITextField with Secure option (password) it does not work at all. Does iOS have any standard way to make background red under text field's stars?

Upvotes: 2

Views: 280

Answers (1)

Stonz2
Stonz2

Reputation: 6396

This worked just fine for me. I created 2 UITextFields, 1 regular and 1 secure, along with a button to take their contents and make their backgrounds attributed red exactly as you have written above:

NSAttributedString *loginString = [[NSAttributedString alloc] initWithString:usernameField.text attributes:@{NSBackgroundColorAttributeName : [UIColor redColor]}];
[usernameField setAttributedText:loginString];

// field set as secure in xib
NSAttributedString *pwString = [[NSAttributedString alloc] initWithString:passwordField.text attributes:@{NSBackgroundColorAttributeName : [UIColor redColor]}];
[passwordField setAttributedText:pwString];

Before:

enter image description here

After:

enter image description here

Upvotes: 2

Related Questions