Change UIFont in secure UITextField strange behaviour in iOS7

5 Solutions Collect From Internet About “Change UIFont in secure UITextField strange behaviour in iOS7”

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

Toggling secure/insecure state with a custom font: to show ‘secure’ text (blob characters), a font containing the blob character is set by iOS. This is the font you’re later seeing render text when you switch to insecure mode.

Though the textfield keeps your assigned custom font, the field’s content is an attributed string which references the blob-containing font.

The simple solution: write to the attributed string field, and your correct, custom font is re-applied.

To turn off secure entry & keep a custom font:

passwordTextField.secureTextEntry = false

// We have to remove iOS' secure font by setting attributedText.
let pwd = passwordTextField.text!
passwordTextField.attributedText = NSAttributedString(string: pwd)

If you turn off “Adjust to Fit” in the password field it will stop doing that resize when you resign the responder.

Set your delegate of your TextField and add this;

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (!textField.secureTextEntry) {
        return YES;
    }

    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if (textField.text.length > 0) {
        textField.font = [UIFont systemFontOfSize:18.0f];
    } else {
        textField.font = [UIFont fontWithName:@"YourFont" size:18.0f];
    }

    return NO;
}

Check this:
UITextField secureTextEntry bullets with a custom font?

There is an elegant solution by glyuck which I liked.
http://github.com/elegion/ELFixSecureTextFieldFont
He created a category on UITextField. To have nicely displayed dots you have to include
ELFixSecureTextFieldFont.h and .m files and then call – (void)fixSecureTextFieldFont on your textField once like this:

[yourCustomSecureTextField fixSecureTextFieldFont];

That is it.