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
):
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()
}
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)
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()
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 :
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];
}
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With