简体   繁体   中英

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

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 "*******"

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 ***

You can use custom UITextField class like this:

UITextField secureTextEntry bullets with a custom font?

Convert objective-c to swift: https://objectivec2swift.com/

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM