Reputation: 6133
I want to set uitextfield as total secure field. However, when user type one character, it is also shown on screen. I would like to hide the initial character also and when user type something (For eg. "A"), it show only secure text(. or *). How shall I do?
Upvotes: 2
Views: 978
Reputation: 900
this is the best solution for achieving behaviour that you describe:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.pinTextField) {
NSString *currentText = textField.text;
NSString *newText = [currentText stringByReplacingCharactersInRange:range withString:string];
textField.text = newText;
return NO;
}
return YES;
}
don't forget to set correctly delegate to your UITextField:
self.myTextField.delegate = self;
Upvotes: 1
Reputation: 57040
Here is a proposal for a solution. Use a second text store for the unhidden text, while replacing the textfield's text from your code, after changing the second text store. Listen to textField:shouldChangeCharactersInRange:replacementString:
as the text field's delegate, perform the changes mentioned, replace the characters with ●
and return NO in the delegate method.
Upvotes: 1
Reputation: 1449
If you don't mind seeing dots instead asterisks, you can set the SecureTextEntry attribute to YES:
[yourUITextField setSecureTextEntry:YES];
Upvotes: 0