ios 如何在 iPhone 应用程序的密码字段中隐藏文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1130953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you obscure text in a password field in an iPhone Application?
提问by Nathaniel Martin
I have a password field in my application (UITextField). When user enters text in the field, I want it to display *
instead of the text they entered.
我的应用程序中有一个密码字段 (UITextField)。当用户在字段中输入文本时,我希望它显示*
而不是他们输入的文本。
I have tried using the UIControlEventEditingDidEnd
of UITextField
but it only shows one *
at the end when editing but I want it to display the *
s as soon as any text is entered, like in any online email password field. How can I get the desired behavior?
我曾尝试使用UIControlEventEditingDidEnd
ofUITextField
但它*
在编辑时只在最后显示一个,但我希望它在*
输入任何文本后立即显示s,就像在任何在线电子邮件密码字段中一样。我怎样才能获得所需的行为?
回答by drvdijk
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:
我不太明白你的问题,但我猜你想要一个 UITextField,它显示字符的点,iPhone 上的每个密码字段都这样做。为此,您希望将该 UITextField 的 secureTextEntry 属性(UITextField 具有这样的属性,因为它符合 UITextInputTraits 协议)设置为 YES:
textfield.secureTextEntry = YES;
回答by Nathaniel Martin
You can also set this in Interface Builder. Select your text field, and check the "secure" setting in the inspector.
您也可以在 Interface Builder 中进行设置。选择您的文本字段,然后检查检查器中的“安全”设置。
回答by Vivekanandan
回答by Donna
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".
如果您使用的是最新版本的 Xcode(版本 9),请单击您想将输入设置为密码的 UITextField,然后单击属性检查器并选中“安全文本输入”。
回答by Mike.R
Swift 3.3
斯威夫特 3.3
textfield.isSecureTextEntry = true