1

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

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
pransu0421
  • 21
  • 1
  • 3
  • In iOS, user are used to see dots instead of stars. That's normal behavior. – Larme Jan 10 '18 at 10:44
  • but i need to display star – pransu0421 Jan 10 '18 at 10:44
  • That's not built for that, you have to do it yourself https://stackoverflow.com/questions/3280069/change-the-secure-password-character-in-uitextfield – Larme Jan 10 '18 at 10:45
  • 3
    Possible duplicate of [How can I change the dots to another character on password field in iOS Swift](https://stackoverflow.com/questions/30403550/how-can-i-change-the-dots-to-another-character-on-password-field-in-ios-swift) – Tamás Sengel Jan 10 '18 at 10:45
  • 1
    One thing you have to realise is that the secure text entry is more than just having a character to hide the text. When you type the original character is shown for a short time and then hidden unless you type quickly when they are hidden as you type. Now if you use your own solution you either lose that or have to replicate it yourself. You really need to ask yourself it it's worth it to have an entry field that looks different to the standard. – Upholder Of Truth Jan 10 '18 at 12:49

4 Answers4

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

Madhur
  • 1,056
  • 9
  • 14
0

You can use custom UITextField class like this:

UITextField secureTextEntry bullets with a custom font?

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

Artem Rizhov
  • 123
  • 9
0

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.

Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23
-2

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.

Alper
  • 3,424
  • 4
  • 39
  • 45