Reputation: 3145
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
Reputation: 6396
This worked just fine for me. I created 2 UITextField
s, 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:
After:
Upvotes: 2