UI文本字段密码 [英] UItextfield password

查看:59
本文介绍了UI文本字段密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以使用以下方法使 UITextfield 在密码模式下运行:

I know that its possible to make UITextfield operate in a password mode by using the following :

textfield.secureTextEntry = YES;

这会将用户输入的所有字符都更改为*".但是,输入的最后一个字符闪烁了大约半秒钟.我了解这是标准行为.

This changes all characters entered by the user into '*'. However, the last character entered flashes up for about half a second. I understand that this is standard behavior.

有什么办法可以阻止这种情况并保持输入的字符完全模糊?

Is there any way of stopping this and keeping the characters entered completely obscured?

为了实现这一点,我想子类化 UITextField 然后覆盖 drawTextInRect:rect

To achieve this, Im thinking of subclassing UITextField and then overriding drawTextInRect:rect

但我欢迎任何其他建议.

But I would welcome any other suggestions.

推荐答案

您可以这样做:

  • 将 UITextField 设置为常规而非秘密.
  • 监听事件 UITextFieldTextDidChangeNotification
  • 当它发生时,将文本字段的字符替换为*"并将真实密码存储在属性中.

这段代码可以运行,虽然有点丑,但它可能可以优化.

This code works, it's kind of ugly, though, it can probably be optimized.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
}

- (void) keyPressed: (NSNotification *) notification {
    if(!self.password)
        self.password = @"";

    NSString* fieldText = [notification.object valueForKey:@"text"];
    NSString* lastChar = [fieldText substringFromIndex:fieldText.length-1];

    if(self.password.length>fieldText.length)
        self.password = [self.password substringToIndex:self.password.length-1];
    else
        self.password = [NSString stringWithFormat:@"%@%@", self.password, lastChar];

    NSString* secret = @"";
    for (int l = 0; l<self.password.length; l++) {
        secret = [secret stringByAppendingString:@"*"];
    }
    self.textField.text = secret;
}

这篇关于UI文本字段密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆