How is a fuzzy safe text entry UITextField in Swift?
When I use the standard security text notation in UITextField in Swift Language after the type type text after UITextField.
Once the focus is lost from the UITextField after trying to change the protected text, then the UITextField is reset first and after it starts putting new text into the UITextField.
How to edit old safe text without saving data to any String object
source to share
I would suggest creating a custom UITextField class and overriding Become First Responder () which will add the functionality you want :
You can override this method on your customers to update the state of an object or perform some action such as highlighting a selection. If you override this method, you must call super on some in your implementation.
The custom class should look like:
class CustomSecureTextField: UITextField {
override func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
if !isSecureTextEntry { return true }
if let currentText = text { insertText(currentText) }
return true
}
}
The implementation logic becomeFirstResponder
looks like this:
By default, a protected text box clears the inserted text when it becomes the first responder text, so what happens in CustomSecureTextField
if the text box is protected, it will insert the currently inserted text - after clearing it, but you have to make sure the text box input is protected (which is the purpose of the addition if !isSecureTextEntry { return true }
), or the text will be duplicated (reinserted) every time the textbox becomes the first responder.
Output:
Note that both text boxes are types CustomSecureTextField
:
source to share
This answer helped me sort out this issue.
textField.isSecureTextEntry = true
the following property will not work if you create the testField property isSecureTextEntrysecure : true .
textField.clearsOnBeginEditing = false
source to share