0

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.

ASCIIThenANSI
  • 865
  • 1
  • 9
  • 27
Julian Osorio
  • 1,116
  • 1
  • 12
  • 30
  • Most places uses the secure text with bullets during complete data entry, then change to the format you specify above after leaving the text field. – Owen Hartnett Apr 21 '15 at 14:48
  • Check this URL. May be you will get answer as you want http://stackoverflow.com/questions/13244313/card-number-first-12-digits-should-be-secure-entry-and-remaining-4-digits-as-nor – Bhoomi Jagani Apr 21 '15 at 15:00

1 Answers1

1

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;
}