Everyone should know thatThe password box has a common requirement:to switch the plain text/cipher text display of the password with a button.Tap to show plaintext.Tap again to show the ciphertext.

It was a very simple requirement,Because uitextfield has the property securetextentry which can be used.

problem found

But never thoughtA simple line of code does not solve the problem:

-(ibaction) secureswitchaction:(id) sender {
 self.passwordtextfield.securetextentry =! self.passwordinputtextfield.securetextentry;

The problem is,When switching between plaintext and ciphertext,If the string widths before and after the switch are different,A large blank appears between the end of the string and the cursor.

After switching,There is a blank space in front of the cursor

It is not mentioned in the official documentation,It is probably a bug in the official implementation.


Fortunately, just add a few lines of code:

-(ibaction) secureswitchaction:(id) sender {
 self.passwordtextfield.securetextentry =! self.passwordtextfield.securetextentry;
 nsstring * text=self.passwordtextfield.text;
 self.passwordtextfield.text [email protected]"";

Can solve the problem.

p.s.I also found an article at that time, using only one line of code[self.passwordinputtextfield becomefirstresponder]; will solve this problem,The only side effect is that it causes the input focus to switch to the password box.If you don't mindThis method is also available.

to sum up

  • Previous Routing and Controller of Homemade PHP Framework
  • Next Detailed usage of ng-include in Angularjs and implementation of multi-tab pages