How to hide passwords in a UITextField like “*****”?

青春壹個敷衍的年華 提交于 2021-01-28 10:44:54

问题


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:

  1. Use a custom font: So something like this: Custom font example.
  2. Use a custom subclass of UITextField: Storing the actual text internally and only showing the 'star' characters.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!