Reputation: 1168
I am trying to create a textfield for entering a credit card number that appears as this:
••••••••••••••••0000
Basically, I need to support secure text entry in the textfield but, only until a given string length. After that point, the text should be displayed normal.
How can I do this?
Thanks.
Upvotes: 0
Views: 641
Reputation:
You can accomplish this using a UITextFieldDelegate
and implementing
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
e.g.
@interface
@property NSString * theActualText;
@property NSInteger numberOfCharactersToObscure;
@implementation
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
self.theActualText = [self.theActualText stringByReplacingCharactersInRange:range withString:string];
NSInteger obscureLength = self.theActualText.length > self.numberOfCharactersToObscure ? self.numberOfCharactersToObscure : self.theActualText.length;
NSRange replaceRange = NSMakeRange(0, obsuceLength);
NSMutableString * replacementString = [NSMutableString new];
for (int i = 0; i < obscureLength; i++) {
[replacementString appendString:@"•"];
}
textField.text = [self.theActualText stringByReplacingCharactersInRange:range withString:replacementString];
return NO;
}
Upvotes: 1