UITextField has trailing whitespace after secureTextEntry toggle

后端 未结 18 1402
逝去的感伤
逝去的感伤 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:16

    To get the cursor to reposition correctly, setting the font attributes seemed to do the trick for me.

    // Hack to update cursor position
    self.passwordTf.defaultTextAttributes = @{NSFontAttributeName: textFieldFont, NSForegroundColorAttributeName: textFieldColor};
    
    // Change secure entry
    self.passwordTf.secureTextEntry = !self.passwordTf.secureTextEntry;
    

    Tested on iOS8, iOS9. Hope it helps!

提交回复
热议问题