iOS 密码明文与密文之间的切换

1,343 阅读1分钟
iOS 密码明文与密文之间的切换,如图所示:


明文


密文


看似复杂,其实也就几行代码

//密码明文
- (IBAction)visibleBtnClick:(UIButton *)sender {    // 切换按钮的状态    
    sender.selected = !sender.selected;    
    if (!sender.selected) { // 明文        
        NSString *tempPwdStr = self.passwordTF.text;        
        self.passwordTF.text = @""; // 这句代码可以防止切换的时候光标偏移        
        self.passwordTF.secureTextEntry = NO;        
        self.passwordTF.text = tempPwdStr;    
    } else { // 密文       
        NSString *tempPwdStr = self.passwordTF.text;        
        self.passwordTF.text = @"";        
        self.passwordTF.secureTextEntry = YES;        
        self.passwordTF.text = tempPwdStr;    
    }
}