BTUIKCardExpiryFormat.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #import "BTUIKCardExpiryFormat.h"
  2. #import "BTUIKUtil.h"
  3. @implementation BTUIKCardExpiryFormat
  4. - (void)formattedValue:(NSString *__autoreleasing *)value cursorLocation:(NSUInteger *)cursorLocation {
  5. if (value == NULL || cursorLocation == NULL) {
  6. return;
  7. }
  8. NSMutableString *s = [NSMutableString stringWithString:self.value];
  9. *cursorLocation = self.cursorLocation;
  10. if (s.length == 0) {
  11. *value = s;
  12. return;
  13. }
  14. if (*cursorLocation == 1 && s.length == 1 && [s characterAtIndex:0] > '1' && [s characterAtIndex:0] <= '9') {
  15. [s insertString:@"0" atIndex:0];
  16. *cursorLocation += 1;
  17. }
  18. if (self.backspace) {
  19. if (*cursorLocation == 2 && s.length == 2) {
  20. [s deleteCharactersInRange:NSMakeRange(1, 1)];
  21. *cursorLocation -= 1;
  22. }
  23. } else {
  24. NSUInteger slashLocation = [s rangeOfString:@"/"].location;
  25. if (slashLocation != NSNotFound) {
  26. if (slashLocation > 2) {
  27. s = [NSMutableString stringWithString:[BTUIKUtil stripNonDigits:s]];
  28. [s insertString:@"/" atIndex:2];
  29. *cursorLocation += 1;
  30. }
  31. } else {
  32. if (s.length >= 2) {
  33. [s insertString:@"/" atIndex:2];
  34. if (*cursorLocation >= 2) {
  35. *cursorLocation += 1;
  36. }
  37. }
  38. }
  39. }
  40. *value = s;
  41. }
  42. @end