Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

ios - Change UIFont in secure UITextField strange behaviour in iOS7

I create a simple project: https://github.com/edzio27/textFieldExample.git

where I add two UITextFields, one with login and second one with secure password. I've noticed there strange behaviour:

  1. click on login and add some text,
  2. click on password and add some text,
  3. click again to login UITextField

enter image description here

Notice that there is a strange behaviour in password font size. It is only appears in iOS7.

What can be the problem?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As a couple people pointed out, it appears secure text fields don't always play well with custom fonts. I worked around this by using the UITextField's UIControlEventEditingChanged to monitor for changes to the textfield and set it to the system font (with the normal looking bullet points) when anything is entered, else use my custom font.

This allows me to keep using my custom font for the textfield placeholder and still look good when the password is entered. The characters that show one-at-a-time while being entered will be the system font, but I'm okay with that.

In viewDidLoad:

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

Now add a textFieldDidChange method:

- (void)textFieldDidChange:(id)sender
{
    UITextField *textField = (UITextField *)sender;

    if (textField == self.passwordTextField) {
        // Set to custom font if the textfield is cleared, else set it to system font
        // This is a workaround because secure text fields don't play well with custom fonts
        if (textField.text.length == 0) {
            textField.font = [UIFont fontWithName:@"OpenSans" size:textField.font.pointSize];
        }
        else {
            textField.font = [UIFont systemFontOfSize:textField.font.pointSize];
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...