I would recommend that you keep two fields, the first, a 12-character limited password field that secures the text.
Limit the length of the field with the following delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 12) ? NO : YES;
}
Set the field to enforce secure text entry:
twelveDigitTextField.secureTextEntry = YES;
The second field, you limit to four characters with the same approach used for the 12-character field. You don't need to protect this field with the secureTextEntry
property.
If you want to make this interface "nice", track the length of the first field with the same delegate method and set the second field as the next responder — in other words, the cursor will jump to the four-character field once the twelve-character field fills up:
/* called within delegate method for 12-character field, once filled up */
[fourDigitTextField becomeFirstResponder];
One reason why you might want to do it this way is to get the same OS-level protections for sensitive data (like a 12-digit card number) as for a password, entered into a password-style UITextField
.
While you can do tricks with replacing text as typewritten, it could be easier to overlook allowing sensitive numbers from this unprotected field to be copied and pasted to another application — whereas with a password field, those limitations are usually in place by default.