iOS input box of UITextField password light and dark text switching method

  • 2021-09-24 23:51:17
  • OfStack

When doing the light and dark text switching (password input box), I met a pit, that is, when switching secureTextEntry, the cursor of the input box will shift. The following lists a solution and a method of light and dark text switching


- (IBAction)pwdTextSwitch:(UIButton *)sender {

  //  Premise : In xib Background diagram of setting the default and selected states of buttons in  
  //  Toggle button status 
  sender.selected = !sender.selected;

  if (sender.selected) { //  If you press it, it will be clear text 

    NSString *tempPwdStr = self.pwdInput.text;
    self.pwdInput.text = @""; //  This code can prevent the cursor from shifting when switching 
    self.pwdInput.secureTextEntry = NO;
    self.pwdInput.text = tempPwdStr;

  } else { //  Secret text 

    NSString *tempPwdStr = self.pwdInput.text;
    self.pwdInput.text = @"";
    self.pwdInput.secureTextEntry = YES;
    self.pwdInput.text = tempPwdStr;
  }
}

Related articles: