Hide password with “•••••••” in a textField

2019-01-30 17:13发布

问题:

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?

回答1:

You can achieve this directly in Xcode:

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


回答2:

in Swift 3.0 or Later

passwordTextField.isSecureTextEntry = true


回答3:

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/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSecureTextField_Class/index.html

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



回答4:

Swift 4 and Xcode Version 9+

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



回答5:

Programmatically (Swift 4)

self.passwordTextField.secure(true)