Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UItextfield password

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?

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

But I would welcome any other suggestions.

like image 273
josully Avatar asked Aug 10 '12 08:08

josully


People also ask

What is secure text field Iphone?

A Boolean value that indicates whether a text object disables copying, and in some cases, prevents recording/broadcasting and also hides the text.


1 Answers

You can do this:

  • Set the UITextField as regular, not secret.
  • Listen to the event UITextFieldTextDidChangeNotification
  • When it happens replace the characters of your textfield for "*" and store the real password in a property.

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;
}
like image 83
Odrakir Avatar answered Oct 06 '22 06:10

Odrakir