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.

Image for entering text for the first time

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.

Image for trying to edit old text

How to edit old safe text without saving data to any String object

+3


source to share


2 answers


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

:

enter image description here

+1


source


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

      

+1


source







All Articles