[英]How to hide the last digit while writing a password in swift
最近我一直在研究 UITextField,它在编辑登录页面、密码字段上的文本时隐藏密码。
众所周知.isSecureTextEntry = true
在一定程度上解决了这个问题。 但是,在输入密码时,最后一个字符会出现,这在某种程度上被视为安全漏洞。 所以我找不到如何解决这个问题的解决方案。 预先感谢您的回答。
我附上两张图片。
今天UIKIT提供的解决方案。
textField.isSecureTextEntry = true
这就是我想要的。
当您输入字符时,func textField(_:shouldChangeCharactersIn:replacementString:)
将调用以用新文本替换旧文本。 所以你只需要捕捉这个值,不要让它默认更新。
textField.isSecureTextEntry = true
textField.delegate = self // add delegate to catch the action
在您的TextField
中更改时获取值,不要让它自动更新 UI。
extension ViewController : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let currentText = self.textField.text as? NSString {
let newText = currentText.replacingCharacters(in: range, with: string) // change to new one
self.textField.text = newText // assign it back to textField text
return false // don't let it update automically
}
return true // by default
}
}
如果屏幕中有多个textField
,则必须检查当前更改的password
是否是您之前的密码。 如果是,则返回false
,否则像默认值一样返回true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.