material-components-ios icon indicating copy to clipboard operation
material-components-ios copied to clipboard

MDCTextField Hide/Show Password

Open Fernandez-Aldo opened this issue 5 years ago • 8 comments

Override trailing view/ (button inside textfield) A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Screen Shot 2020-01-14 at 2 55 17 PM I'm trying to accomplish this functionality. I'm able to show the password but unable to hide it again. It seems there is a preloaded button inside the texfield that need to be overriden. Can you help me please?

Internal data

Fernandez-Aldo avatar Jan 14 '20 22:01 Fernandez-Aldo

The title doesn't have a [Component] prefix.

Hello. Following up this post to see if you have a fix for it

Fernandez-Aldo avatar Jan 17 '20 23:01 Fernandez-Aldo

You're trying to make it so that tapping the rightView toggles secureTextEntry on the textfield, right? If that's right, then I think all you need to do is create a button, set it as the rightView, then wire it up to call a method that toggles the secureTextEntry on the textfield, right?

andrewoverton avatar Jan 21 '20 16:01 andrewoverton

That's exactly what I'm doing. It works to unhide the password but then, when I try to click it again there is no button at all. I've debugged it and the button disappears. I think the MDCTexfield has already set that button to hide when clicked it.

Fernandez-Aldo avatar Jan 21 '20 17:01 Fernandez-Aldo

In https://github.com/material-components/material-components-ios/issues/9381 you mentioned you're working with MDCOutlinedTextField, but the picture in the description for this issue looks like an MDCTextField with an MDCTextInputControllerOutlined. Are you using both types of textfields (one for that issue and one for this issue) or are you just using one?

If the issue you're describing here pertains to the MDCTextField like the image in the description suggests I would recommend trying the new MDCOutlinedTextField. If the issue is with MDCOutlinedTextField I can look into that.

andrewoverton avatar Jan 21 '20 17:01 andrewoverton

I´m using an MDCTextField with an MDCTextInputControllerOutlined. I don´t know the difference of both sorry. How can I make use of the MDCOutlinedTexfield?

Fernandez-Aldo avatar Jan 21 '20 18:01 Fernandez-Aldo

No worries, one is totally new. If you're interested in trying the new one, MDCOutlinedTextFIeld, which I would recommend over the old one, you can find instructions here: https://github.com/material-components/material-components-ios/tree/develop/components/TextControls. I would check if the issue exists in the new one.

andrewoverton avatar Jan 22 '20 16:01 andrewoverton

Just a help for other looking for a similar implementation with MDCOutlinedTexfield, tested on iOS 12.4+, other implementations had problems with iOS 12.4.

Replace the imageOpenEye, imageCloseEye with your images, I use a wrapper on top of SVGKit in my application as of now.

    let imageOpenEye = svgForImageName("PasswordOpen", withColor: UIColor.black)
    let imageCloseEye = svgForImageName("EyeCloseIconSelected", withColor: UIColor.black)
    
    let passwordButton = UIButton(type: .custom)
    passwordButton.frame = CGRect(x: CGFloat(0), y: CGFloat(0),
                                  width: CGFloat(45), height: CGFloat(passwordTextField.frame.height))
    passwordButton.setImage(imageOpenEye, for: .normal)
    passwordButton.setImage(imageCloseEye, for: .selected)
    passwordButton.addTarget(self, action: #selector(passViewTap), for: .touchUpInside)
    
    passwordTextField.trailingView = passwordButton
    passwordTextField.trailingViewMode = .always
    passwordTextField.isSecureTextEntry = true
    passwordTextField.leadingUnderlineLabel.lineBreakMode = .byWordWrapping

    @objc func passViewTap(sender: UIButton) {
      sender.isSelected = !sender.isSelected
      txtCurrentPassword.isSecureTextEntry = !sender.isSelected
    }

brosahay avatar Oct 27 '20 19:10 brosahay