如何在 iPhone 应用程序的密码字段中隐藏文本?

我的应用程序中有一个密码字段(UITextField)。当用户在字段中输入文本时,我希望它显示 *,而不是他们输入的文本。

我已经尝试使用 UITextFieldUIControlEventEditingDidEnd,但它只显示一个 *在结束时编辑,但我希望它显示的 *尽快任何文本输入,就像在任何在线电子邮件密码字段。我怎样才能得到想要的行为?

67678 次浏览

I don't really understand your question, but I'm guessing you want a UITextField which displays dots for the characters, which every password field on the iPhone does. For this, you want to set the secureTextEntry property of that UITextField (UITextField has such a property because it conforms to the UITextInputTraits protocol) to YES:

textfield.secureTextEntry = YES;

You can also set this in Interface Builder. Select your text field, and check the "secure" setting in the inspector.

secure setting in Interface Builder

You can add a key path secureTextEntry of type Boolean and tick it in the User defined runtime attributes in the Identity Inspector.

enter image description here

If you are using the latest version of Xcode, (version 9), click on the UITextField that you want to set the input as password, then click on the attributes inspector and check "Secure Text Entry".

Swift 3.3

textfield.isSecureTextEntry = true