Hide password with "•••••••" in a textField

SwiftPasswords

Swift Problem Overview


In my app there is a textField where the user have to put is password in and i want that when he enter a character it change it to '•' how can i do this?

Swift Solutions


Solution 1 - Swift

You can achieve this directly in Xcode:

enter image description here

The very last checkbox, make sure secure is checked .

Or you can do it using code:

Identifies whether the text object should hide the text being entered.

Declaration

optional var secureTextEntry: Bool { get set }

Discussion

This property is set to false by default. Setting this property to true creates a password-style text object, which hides the text being entered.

example:

texfield.secureTextEntry = true

Solution 2 - Swift

in Swift 3.0 or Later

passwordTextField.isSecureTextEntry = true

Solution 3 - Swift

Swift 4 and Xcode Version 9+

Can be set via "Secure Text Entry" via Interface Builder

Secure Text Entry checked via storyboard screenshot

Solution 4 - Swift

In Xcode 6.3.1, if you use a NSTextField you will not see the checkbox for secure.

Instead of using NSTextField use NSSecureTextField

https://developer.apple.com/documentation/appkit/nssecuretextfield

I'm guessing this is a Swift/Objective-C change since there is now a class for secure text fields. In the above link it says Available in OS X v10.0 and later. If you know more about when/why/what versions of Swift/Objective-C, Xcode, or OS X this

Solution 5 - Swift

For SwiftUI, try

TextField ("Email", text: $email)
    .textFieldStyle(RoundedBorderTextFieldStyle()).padding()
SecureField ("Password", text: $password)
    .textFieldStyle(RoundedBorderTextFieldStyle()).padding()

Solution 6 - Swift

Programmatically (Swift 4 & 5)

self.passwordTextField.isSecureTextEntry = true

Solution 7 - Swift

You can do this by using properties of textfield from Attribute inspector

Tap on Your Textfield from storyboard and go to Attribute inspector , and just check the checkbox of "Secure Text Entry" SS is added for graphical overview to achieve same

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClément BisaillonView Question on Stackoverflow
Solution 1 - SwiftmedaView Answer on Stackoverflow
Solution 2 - SwiftNandkishor mewaraView Answer on Stackoverflow
Solution 3 - SwiftHatimView Answer on Stackoverflow
Solution 4 - Swiftuser3731622View Answer on Stackoverflow
Solution 5 - SwiftphilView Answer on Stackoverflow
Solution 6 - SwiftArunabh DasView Answer on Stackoverflow
Solution 7 - SwiftVicky DungraneeView Answer on Stackoverflow