Levi
Levi

Reputation: 7343

Change behaviour of secure UITextField

I need a secure UITextField, but by default it is showing the last character for about 1 sec before it turns to *. Is there any way to disable this, and to see * instead of the character as soon as you type it in?

Upvotes: 1

Views: 332

Answers (2)

Levi
Levi

Reputation: 7343

I solved it using a workaround, couldn't find a simple solution like setting a flag:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.mySecureTextField) {
        NSString *currentText = textField.text;
        NSString *newText = [currentText stringByReplacingCharactersInRange:range withString:string];
        textField.text = newText;
        return NO;
    }
    return YES;
}

Upvotes: 1

MinuMaster
MinuMaster

Reputation: 1457

Yes you can perform that. For that you need to add white space in Text field did begin editing. And when user end editing remove that white space.

See below code snippet for more details.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.text = @" ";
    UITextPosition *beginning = [textField beginningOfDocument];
    [textField setSelectedTextRange:[textField textRangeFromPosition:beginning toPosition:beginning]];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text length] > 0)
        textField.text = [textField.text substringToIndex:[textField.text length] - 1];;
}

Let me know if you have query on that.

Thanks.

Upvotes: 1

Related Questions