如何在像“*****"这样的 UITextField 中隐藏密码? [英] How to hide passwords in a UITextField like "*****"?

查看:67
本文介绍了如何在像“*****"这样的 UITextField 中隐藏密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 UITextField 中隐藏密码,例如 "*****"?

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

推荐答案

要实现相同的目的,您可以使用普通文本字段而无需安全输入选项.当用户输入一个字符时,将其保存到一个字符串变量中,并在文本字段中将其替换为您希望显示的字符而不是项目符号.

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.

将 viewDidLoad() 中的 textField 委托设置为:

Set the textField delegate in viewDidLoad() as:

textField.delegate = self

然后简单地使用 shouldChangeCharactersInRange() 如下:

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
     }

textField 中的输出将是 ***

The output in the textField will be the ***

这篇关于如何在像“*****"这样的 UITextField 中隐藏密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆