问题
How to hide passwords in a UITextField
like "*****"?
To hide password, i used following code
txtPassword.isSecureTextEntry = true
but this will display like “•••••••”, but i need like "*******"
回答1:
To achieve the same, you can use a normal textfield without the secure input option. When a user enters a character, save it to a string variable, and replace it in the textfield with the character you wish to present instead of the bullets.
Set the textField delegate in viewDidLoad() as:
textField.delegate = self
and then simply use the shouldChangeCharactersInRange() as follows:
Here's the code (will show the password as *** ):
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
password = password+string
textField.text = textField.text!+"*"
return false
}
The output in the textField will be the ***
回答2:
You can use custom UITextField class like this:
UITextField secureTextEntry bullets with a custom font?
Convert objective-c to swift: https://objectivec2swift.com/
回答3:
Right for clarity there are basically three options:
- Use a custom font: So something like this: Custom font example.
- Use a custom subclass of UITextField: Storing the actual text internally and only showing the 'star' characters.
- Don't do it: Users expect a consistent experience across the entire device and you can lose other functionality (see below).
Now one thing to be aware of is that a secure entry UITextField does more than just only display the 'dot' characters. If you watch how it works when you enter a character it displays for a short time and then is replaced by the 'dot' thus giving the user time to see they entered the correct character. If you type quickly then all characters are replaced with a 'dot' immediately that the next one is added. If you do your own version to replace the 'dot' character you either have to replicate that functionality or lose it.
回答4:
The easiest way to get the behavior you want is to use some kind of custom password field that allows this customization.
I would not recommend it.
来源:https://stackoverflow.com/questions/48185533/how-to-hide-passwords-in-a-uitextfield-like