UITextField has trailing whitespace after secureTextEntry toggle

后端 未结 18 1220
逝去的感伤
逝去的感伤 2020-12-29 19:27

I have a button that toggles between Show/Hide mode (i.e. toggles a UITextField between secureTextEntry NO and YES). The purpose of which is to allow the user to see the pa

相关标签:
18条回答
  • 2020-12-29 20:19

    Swift UITextField extension:

    extension UITextField {
        func toggleSecureEntry() {
            let wasFirstResponder = isFirstResponder
    
            if wasFirstResponder { resignFirstResponder() }
            isSecureTextEntry.toggle()
            if wasFirstResponder { becomeFirstResponder() }
        }
    }
    

    Setting textField.text solution also works in some situations but not for my need (Custom font with two text fields. Caused font changes and glitches on runtime.) Adding here too.

    func toggleSecureEntry() {
        isSecureTextEntry.toggle()
        let originalText = text
        text = nil
        text = originalText
    }
    
    0 讨论(0)
  • 2020-12-29 20:22

    Here is the solution:

    - (void)showHidePassword:(UIButton *)sender {
        EDSignUpCell *cell = [self.signUpTblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        if(!TRIM_SPACE(cell.cellTextField.text).length) {return;}
        [cell.showHidePasswordBtn setSelected:!cell.showHidePasswordBtn.isSelected];
        cell.cellTextField.secureTextEntry = cell.showHidePasswordBtn.isSelected;
        [cell.cellTextField setText:cell.cellTextField.text];
        [cell.cellTextField becomeFirstResponder];
    }
    
    0 讨论(0)
  • 2020-12-29 20:25

    This Works in my case

        BOOL wasFirstResponder = [self.passwordTextField isFirstResponder];
        if([self.passwordTextField isSecureTextEntry])
        {
            //This three lines are key
            self.passwordTextField.delegate = nil;
            [self.passwordTextField resignFirstResponder];
            self.passwordTextField.delegate = self;
        }
    
        [self.passwordTextField setSecureTextEntry: !self.passwordTextField.isSecureTextEntry];
        if(wasFirstResponder)
            [self.passwordTextField becomeFirstResponder];
    
    0 讨论(0)
  • 2020-12-29 20:27

    It appears that the second solution in the referenced link, when implemented, has the desired behavior of not adding an extra space:

    https://stackoverflow.com/a/8495888/738190

    0 讨论(0)
  • 2020-12-29 20:28

    PRE-iOS-8.0 (dated solution)... In your button's action method (toggling between secureTextEntry YES and NO), simply set UITextField's text property to its current text value. Although this may seem redundant and a bit like a hack, this will redraw the cursor in the right position. Here's an example of what your button's action method should look like now...

    -(void)toggleButtonPressed:(UIButton*)sender
    {
        // Toggle between secure and not-so-secure entry
        self.toggleButton.selected = !self.toggleButton.selected;
        self.textfield.secureTextEntry = !self.toggleButton.selected;
    
        // * Redundant (but necessary) line *
        [self.textfield setText:self.textfield.text];
    }
    

    POST-iOS-8.0... As of iOS 8.0, it appears that UITextField's text setter no longer redraws the cursor when called with a string equal to its current string value. Now, we need to take this a step further and actually change the text value before resetting it again. Replace the above setText: line with something like these lines.

    // * Extra redundant (but necessary) lines *
    NSString *currentText = self.textfield.text;
    [self.textfield setText:@"Arbitrary string..."]; // Change the value
    [self.textfield setText:currentText]; // Reset the value
    
    0 讨论(0)
  • 2020-12-29 20:34

    I have a clean solution not going dirty with text property of UITextField.

    Wrap them in this style.

    [self.passwordTextField resignFirstResponder]; // first resign its first responder.
    
    // change `secureTextEntry` property's value if necessary.
    
    if (self.passwordTextField.secureTextEntry) {
        self.passwordTextField.secureTextEntry = NO;
        self.passwordEcryptButton.selected = YES;
    }else{
        self.passwordTextField.secureTextEntry = YES;
        self.passwordEcryptButton.selected = NO;
    }
    
    [self.passwordTextField becomeFirstResponder];  // finally gain its first responder again.
    
    0 讨论(0)
提交回复
热议问题