明暗文切换(密码输入框)遇到的坑

在做明暗文切换(密码输入框)的时候遇见一个坑,就是切换secureTextEntry的时候,输入框的光标会偏移,下面列出了一个解决办法及一种明暗文切换的方法

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

    // 前提:在xib中设置按钮的默认与选中状态的背景图 

    // 切换按钮的状态

    sender.selected = !sender.selected;

    if (sender.selected) { // 按下去了就是明文

        NSString *tempPwdStr = self.pwdInput.text;

        self.pwdInput.text = @"";  // 这句代码可以防止切换的时候光标偏移

        self.pwdInput.secureTextEntry = NO;

        self.pwdInput.text = tempPwdStr;

    } else { // 暗文

        NSString *tempPwdStr = self.pwdInput.text;

        self.pwdInput.text = @"";

        self.pwdInput.secureTextEntry = YES;

        self.pwdInput.text = tempPwdStr;

    }

}

原文地址:https://www.cnblogs.com/wzl666/p/9083810.html