How to toggle a UITextField secure text entry (hide password) in Swift?

前端 未结 24 2298
时光取名叫无心
时光取名叫无心 2021-01-30 02:00

I currently have a UITextfield with an eye icon in it that when pressed is supposed to toggle the secure text entry on and off.

I know you can che

相关标签:
24条回答
  • 2021-01-30 03:04

    Hope this is simpler solution rather than creating a BOOL object globally.

    @IBAction func passwordToggleButton(sender: UIButton) {
        let isSecureTextEntry = passwordTextField.isSecureTextEntry
        passwordTextField.isSecureTextEntry = isSecureTextEntry ? false : true
        if isSecureTextEntry {
            visibilityButton.setImage(UIImage(named: "visibility"), for: .normal)
        } else {
            visibilityButton.setImage(UIImage(named: "visibility_off"), for: .normal)
        }
    }
    
    0 讨论(0)
  • 2021-01-30 03:05

    An unintended side-effect of this is that if the user toggles to insecure, and then back to secure, the existing text will be cleared if the user continues typing. The cursor may also end up in the wrong position unless we reset the selected text range.

    Below is an implementation that handles these cases (Swift 4)

    extension UITextField {
        func togglePasswordVisibility() {
            isSecureTextEntry = !isSecureTextEntry
    
            if let existingText = text, isSecureTextEntry {
                /* When toggling to secure text, all text will be purged if the user 
                 continues typing unless we intervene. This is prevented by first 
                 deleting the existing text and then recovering the original text. */
                deleteBackward()
    
                if let textRange = textRange(from: beginningOfDocument, to: endOfDocument) {
                    replace(textRange, withText: existingText)
                }
            }
    
            /* Reset the selected text range since the cursor can end up in the wrong
             position after a toggle because the text might vary in width */
            if let existingSelectedTextRange = selectedTextRange {
                selectedTextRange = nil
                selectedTextRange = existingSelectedTextRange
            }
        }
    }
    

    This snippet is using the replace(_:withText:) function because it triggers the .editingChanged event, which happens to be useful in my application. Just setting text = existingText should be fine as well.

    0 讨论(0)
  • 2021-01-30 03:06

    only add this line into your code replace you TextField name with "textfield" Done: you need to change the isSecureTextEntry propertity to change true for password type textFiled like ......

    textField.isSecureTextEntry = true
    
    0 讨论(0)
  • 2021-01-30 03:07

    Why to use an extra var. In the action method of the eye button just do as below

    password.secureTextEntry = !password.secureTextEntry
    

    UPDATE

    Swift 4.2 (as per @ROC comment)

    password.isSecureTextEntry.toggle()
    
    0 讨论(0)
  • 2021-01-30 03:07

    Here is your answer no need to take any bool var:

    @IBAction func showHideAction(sender: AnyObject) {
    
            if tfPassword.secureTextEntry{
                tfPassword.secureTextEntry = false
    
            }else{
                tfPassword.secureTextEntry = true;
            } 
        }
    
    0 讨论(0)
  • 2021-01-30 03:07
    @objc func togglePasscode(){
    
       switch textfield.isSecureTextEntry{
          case true:
             textfield.isSecureTextEntry = false 
    
          case false:
             textfield.isSecureTextEntry = true 
       }
    }
    

    Here is a easy and more readable solution using Switch statement.

    0 讨论(0)
提交回复
热议问题