iOS密码明文暗文(隐藏与显示)

- (IBAction)showOrHideBtnClick:(UIButton *)sender {
    // 前提:在xib中设置按钮的默认与选中状态的背景图
    // 切换按钮的状态
    sender.selected = !sender.selected;
    
    if (sender.selected) { // 按下去了就是明文
        
        NSString *tempPwdStr = self.passwordTF.text;
        self.passwordTF.text = @""; // 这句代码可以防止切换的时候光标偏移
        self.passwordTF.secureTextEntry = NO;
        self.passwordTF.text = tempPwdStr;
        [sender setImage:[UIImage imageNamed:@"eye_open"] forState:UIControlStateNormal];
    } else { // 暗文
        
        NSString *tempPwdStr = self.passwordTF.text;
        self.passwordTF.text = @"";
        self.passwordTF.secureTextEntry = YES;
        self.passwordTF.text = tempPwdStr;
        
        [sender setImage:[UIImage imageNamed:@"eye_close"] forState:UIControlStateNormal];
    }
    
}

你可能感兴趣的:(iOS密码明文暗文(隐藏与显示))