27

I have an UITextField which I use as a password field. It has by default secureTextEntry set to true. I also have a UIButton to toggle the show/hide of the password.

When I change the textfield from secureTextEntry set to true to false, the font gets weird. Seems it becomes Times New Roman or similar.

I have tried re-setting the font to system with size 14, but it didn't change anything.

Example of what happens (with initial secureTextEntry set to true): Example

My code:

@IBAction func showHidePwd(sender: AnyObject) {
    textfieldPassword.secureTextEntry = !textfieldPassword.secureTextEntry

    // Workaround for dot+whitespace problem
    if !textfieldPassword.secureTextEntry {
        let tempString = textfieldPassword.text
        textfieldPassword.text = nil
        textfieldPassword.text = tempString
    }
    textfieldPassword.font = UIFont.systemFontOfSize(14)

    if textfieldPassword.secureTextEntry {
        showHideButton.setImage(UIImage(named: "EyeClosed"), forState: .Normal)
    } else {
        showHideButton.setImage(UIImage(named: "EyeOpen"), forState: .Normal)
    }

    textfieldPassword.becomeFirstResponder()
}
3
  • Have you tried using a NSAttributedString to update it?
    – aramusss
    Feb 9, 2016 at 13:46
  • No, I haven't. It seem a bit overkill as well =S Feb 9, 2016 at 13:47
  • 2
    FWIW, this seems to be fixed as of iOS 10.
    – Cyrille
    Dec 15, 2016 at 13:24

7 Answers 7

34

Changing the font of UITextField will not take effect until you first set the font to nil. Try following.

textfieldPassword.font = nil
textfieldPassword.font = UIFont.systemFontOfSize(14.0)
3
  • 1
    This actually solved my problem! Thanks. However, its still weird that I need to do this since I am not changing the font anywhere else. Feb 9, 2016 at 15:38
  • Does anyone know why the font has to be de-initialized before being changed in UIKit?
    – Spilly
    Oct 6, 2016 at 0:21
  • 1
    This is complete nonsense, I wasted an hour.
    – CFIFok
    Aug 8, 2018 at 10:02
14

I've run into this error before. Not sure why it happens. I have found the if you dismiss the keyboard and then toggle secureTextEntry, you won't lose the font style.

textField.resignFirstResponder()
textField.secureTextEntry = !self.textField.secureTextEntry
textField.becomeFirstResponder()
1
  • 2
    This was a better solution for me than the accepted answer, because it doesn't override the system font for the secure text entry dots.
    – cargath
    Apr 3, 2017 at 13:21
5

In fact to solve the problem I used a mix of the two previous responses.

In my particular case, my view contains only two fields. So I :

  • Memorize the field who has the focus (if any).
  • If the field switch to "not secure", I set the font to nil, set the font back to its original value, and resignFirstResponder on text field.
  • If the field switch "to secure", I just resignFirstResponder on the text field.
  • In all cases, I restore the focus to the original field (if any).

With this method, I have not problem of show/hide keyboard, it works perfectly.

Regards. Sébastien.

BOOL loginTextFieldHadFocus = self.loginTextField.isFirstResponder;
BOOL passwordTextFieldHadFocus = self.passwordTextField.isFirstResponder;
if (self.passwordTextField.isSecureTextEntry)
{
    self.passwordTextField.secureTextEntry = NO;
    self.passwordTextField.font = nil;
    self.passwordTextField.font = [UIFont systemFontOfSize:19.0 weight:UIFontWeightRegular];
    [self.passwordTextField resignFirstResponder];
}
else
{
    self.passwordTextField.secureTextEntry = YES;
    [self.passwordTextField resignFirstResponder];
}
if (loginTextFieldHadFocus)
{
    [self.loginTextField becomeFirstResponder];
}
else if (passwordTextFieldHadFocus)
{
    [self.passwordTextField becomeFirstResponder];
}
2

For swift, Set a bool property to Show and hide the password after that assign false from view did load method , then do the following code when password Show/Hide button is clicked :

    @IBAction func showHidePassword(sender: UIButton) {
    showPassword = !showPassword
    passwordTxtField.becomeFirstResponder()
    if (showPassword == true) {
        passwordTxtField.secureTextEntry = false
        let password = passwordTxtField.text!
        passwordTxtField.attributedText = NSAttributedString(string: password)

    }else{
        passwordTxtField.secureTextEntry = true
    }
    sender.setTitle(showPassword == true ? "Hide" : "Show", forState: .Normal)
}
0
passwordTextField.secureTextEntry = false
let text = passwordTextField.text!
passwordTextField.attributedText = NSAttributedString(string: text)

this does work for me. This we are just assigning font using attributed string. However, there seems a small glitch.

0

These bug could be fixed in this way:

[self resignFirstResponder];
NSString *text = self.text;
self.text = @" ";
self.text = text;

//here add some logic

[self becomeFirstResponder];
0

On toggle secure text entry, setting font to nil and back to normal helped me:

@objc func toggleSecureTextEntry(_ sender: UIButton) {
    isSecureTextEntry = !isSecureTextEntry
    updateShowButtonVisual()

    if !isSecureTextEntry {
        let oldFont = font
        font = nil
        font = oldFont
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.