From ac89fb039d1d636a4cfe8f978bbd6479cf683616 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 22 Jun 2015 18:35:13 +0100 Subject: [PATCH] Added simple credit card additions --- .../Inputs/InputsFormViewController.m | 14 +++++++ XLForm/XL/Cell/XLFormTextFieldCell.m | 41 +++++++++++++++++++ XLForm/XL/Controllers/XLFormViewController.m | 3 +- XLForm/XL/Descriptors/XLFormDescriptor.m | 4 +- XLForm/XL/Descriptors/XLFormRowDescriptor.h | 2 + XLForm/XL/Descriptors/XLFormRowDescriptor.m | 6 ++- .../XL/Descriptors/XLFormSectionDescriptor.m | 10 ++--- XLForm/XL/XLForm.h | 1 + XLForm/XL/XLForm.m | 2 +- 9 files changed, 72 insertions(+), 11 deletions(-) diff --git a/Examples/Objective-C/Examples/Inputs/InputsFormViewController.m b/Examples/Objective-C/Examples/Inputs/InputsFormViewController.m index 4a88c659..bb6b5929 100644 --- a/Examples/Objective-C/Examples/Inputs/InputsFormViewController.m +++ b/Examples/Objective-C/Examples/Inputs/InputsFormViewController.m @@ -38,6 +38,8 @@ NSString *const kUrl = @"url"; NSString *const kTextView = @"textView"; NSString *const kNotes = @"notes"; +NSString *const kCCLast4Digits = @"ccLast4Digits"; +NSString *const kCCExpiryDate = @"ccExpiryDate"; @implementation InputsFormViewController @@ -110,6 +112,18 @@ -(id)init row = [XLFormRowDescriptor formRowDescriptorWithTag:kNotes rowType:XLFormRowDescriptorTypeTextView title:@"Notes"]; [section addFormRow:row]; + // Credit Card additions + section = [XLFormSectionDescriptor formSectionWithTitle:@"Credit Card Example"]; + [formDescriptor addFormSection:section]; + + row = [XLFormRowDescriptor formRowDescriptorWithTag:kCCLast4Digits rowType:XLFormRowDescriptorTypeInteger title:@"Last 4 Digits"]; + row.maxLength = 4; + [section addFormRow:row]; + + row = [XLFormRowDescriptor formRowDescriptorWithTag:kCCExpiryDate rowType:XLFormRowDescriptorTypeCreditCardExpiryDate title:@"Expiry Date"]; + [row.cellConfigAtConfigure setObject:@"MM/YY" forKey:@"textField.placeholder"]; + [section addFormRow:row]; + return [super initWithForm:formDescriptor]; } diff --git a/XLForm/XL/Cell/XLFormTextFieldCell.m b/XLForm/XL/Cell/XLFormTextFieldCell.m index 73c2a97b..0e1d386f 100644 --- a/XLForm/XL/Cell/XLFormTextFieldCell.m +++ b/XLForm/XL/Cell/XLFormTextFieldCell.m @@ -128,6 +128,12 @@ -(void)update self.textField.autocorrectionType = UITextAutocorrectionTypeNo; self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone; } + else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCreditCardExpiryDate]){ + self.textField.keyboardType = UIKeyboardTypeNumberPad; + self.textField.autocorrectionType = UITextAutocorrectionTypeNo; + self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone; + self.rowDescriptor.maxLength = 5; + } self.textLabel.text = ((self.rowDescriptor.required && self.rowDescriptor.title && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle) ? [NSString stringWithFormat:@"%@*", self.rowDescriptor.title] : self.rowDescriptor.title); @@ -240,6 +246,18 @@ - (BOOL)textFieldShouldEndEditing:(UITextField *)textField } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { + + if (self.rowDescriptor.maxLength > 0) { + // Prevent crashing undo bug – see note below. + if (range.length + range.location > textField.text.length) + { + return NO; + } + + NSUInteger newLength = [textField.text length] + [string length] - range.length; + return (newLength < self.rowDescriptor.maxLength + 1) ? YES : NO; + } + return [self.formViewController textField:textField shouldChangeCharactersInRange:range replacementString:string]; } @@ -256,6 +274,22 @@ - (void)textFieldDidEndEditing:(UITextField *)textField [self.formViewController textFieldDidEndEditing:textField]; } +- (NSString *)formatCreditCardExpiry:(NSString *)text +{ + static NSString *previousText = @""; + + if (text.length == 2) { + if (previousText.length < text.length) { + text = [NSString stringWithFormat:@"%@/", text]; + } else { + text = [text substringToIndex:1]; + } + } + + previousText = text; + + return text; +} #pragma mark - Helper @@ -271,6 +305,13 @@ - (void)textFieldDidChange:(UITextField *)textField{ } else { self.rowDescriptor.value = nil; } + + if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCreditCardExpiryDate]) { + NSString *formattedText = [self formatCreditCardExpiry:self.textField.text]; + if (![formattedText isEqualToString:self.textField.text]) { + self.textField.text = formattedText; + } + } } -(void)setReturnKeyType:(UIReturnKeyType)returnKeyType diff --git a/XLForm/XL/Controllers/XLFormViewController.m b/XLForm/XL/Controllers/XLFormViewController.m index a233abe6..87f7c3a3 100755 --- a/XLForm/XL/Controllers/XLFormViewController.m +++ b/XLForm/XL/Controllers/XLFormViewController.m @@ -238,7 +238,8 @@ +(NSMutableDictionary *)cellClassesForRowDescriptorTypes XLFormRowDescriptorTypePicker : [XLFormPickerCell class], XLFormRowDescriptorTypeSlider : [XLFormSliderCell class], XLFormRowDescriptorTypeSelectorLeftRight : [XLFormLeftRightSelectorCell class], - XLFormRowDescriptorTypeStepCounter: [XLFormStepCounterCell class] + XLFormRowDescriptorTypeStepCounter: [XLFormStepCounterCell class], + XLFormRowDescriptorTypeCreditCardExpiryDate: [XLFormTextFieldCell class] } mutableCopy]; }); return _cellClassesForRowDescriptorTypes; diff --git a/XLForm/XL/Descriptors/XLFormDescriptor.m b/XLForm/XL/Descriptors/XLFormDescriptor.m index 58dbc437..42670700 100644 --- a/XLForm/XL/Descriptors/XLFormDescriptor.m +++ b/XLForm/XL/Descriptors/XLFormDescriptor.m @@ -85,12 +85,12 @@ -(instancetype)initWithTitle:(NSString *)title; +(instancetype)formDescriptor { - return [[self class] formDescriptorWithTitle:nil]; + return [self formDescriptorWithTitle:nil]; } +(instancetype)formDescriptorWithTitle:(NSString *)title { - return [[[self class] alloc] initWithTitle:title]; + return [[XLFormDescriptor alloc] initWithTitle:title]; } -(void)addFormSection:(XLFormSectionDescriptor *)formSection diff --git a/XLForm/XL/Descriptors/XLFormRowDescriptor.h b/XLForm/XL/Descriptors/XLFormRowDescriptor.h index 1c350385..ff0d4d46 100644 --- a/XLForm/XL/Descriptors/XLFormRowDescriptor.h +++ b/XLForm/XL/Descriptors/XLFormRowDescriptor.h @@ -50,6 +50,7 @@ typedef NS_ENUM(NSUInteger, XLFormPresentationMode) { @property (nonatomic) id value; @property Class valueTransformer; @property UITableViewCellStyle cellStyle; +@property int maxLength; @property (nonatomic) NSMutableDictionary *cellConfig; @property (nonatomic) NSMutableDictionary *cellConfigIfDisabled; @@ -141,3 +142,4 @@ typedef NS_ENUM(NSUInteger, XLFormPresentationMode) { @property (nonatomic, strong) Class formSegueClass; @end + diff --git a/XLForm/XL/Descriptors/XLFormRowDescriptor.m b/XLForm/XL/Descriptors/XLFormRowDescriptor.m index b32506b5..9305d4dd 100644 --- a/XLForm/XL/Descriptors/XLFormRowDescriptor.m +++ b/XLForm/XL/Descriptors/XLFormRowDescriptor.m @@ -81,6 +81,7 @@ -(instancetype)initWithTag:(NSString *)tag rowType:(NSString *)rowType title:(NS _rowType = rowType; _title = title; _cellStyle = UITableViewCellStyleValue1; + _maxLength = 0; _validators = [NSMutableArray new]; _cellConfig = [NSMutableDictionary dictionary]; _cellConfigIfDisabled = [NSMutableDictionary dictionary]; @@ -98,12 +99,12 @@ -(instancetype)initWithTag:(NSString *)tag rowType:(NSString *)rowType title:(NS +(instancetype)formRowDescriptorWithTag:(NSString *)tag rowType:(NSString *)rowType { - return [[self class] formRowDescriptorWithTag:tag rowType:rowType title:nil]; + return [XLFormRowDescriptor formRowDescriptorWithTag:tag rowType:rowType title:nil]; } +(instancetype)formRowDescriptorWithTag:(NSString *)tag rowType:(NSString *)rowType title:(NSString *)title { - return [[[self class] alloc] initWithTag:tag rowType:rowType title:title]; + return [[XLFormRowDescriptor alloc] initWithTag:tag rowType:rowType title:title]; } -(XLFormBaseCell *)cellForFormController:(XLFormViewController *)formController @@ -590,3 +591,4 @@ -(void)setFormSegueIdenfifier:(NSString *)formSegueIdenfifier } @end + diff --git a/XLForm/XL/Descriptors/XLFormSectionDescriptor.m b/XLForm/XL/Descriptors/XLFormSectionDescriptor.m index c15c6bef..c2c29b46 100644 --- a/XLForm/XL/Descriptors/XLFormSectionDescriptor.m +++ b/XLForm/XL/Descriptors/XLFormSectionDescriptor.m @@ -95,27 +95,27 @@ -(instancetype)initWithTitle:(NSString *)title sectionOptions:(XLFormSectionOpti +(instancetype)formSection { - return [[self class] formSectionWithTitle:nil]; + return [self formSectionWithTitle:nil]; } +(instancetype)formSectionWithTitle:(NSString *)title { - return [[self class] formSectionWithTitle:title sectionOptions:XLFormSectionOptionNone]; + return [self formSectionWithTitle:title sectionOptions:XLFormSectionOptionNone]; } +(instancetype)formSectionWithTitle:(NSString *)title multivaluedSection:(BOOL)multivaluedSection { - return [[self class] formSectionWithTitle:title sectionOptions:(multivaluedSection ? XLFormSectionOptionCanInsert | XLFormSectionOptionCanDelete : XLFormSectionOptionNone)]; + return [self formSectionWithTitle:title sectionOptions:(multivaluedSection ? XLFormSectionOptionCanInsert | XLFormSectionOptionCanDelete : XLFormSectionOptionNone)]; } +(instancetype)formSectionWithTitle:(NSString *)title sectionOptions:(XLFormSectionOptions)sectionOptions { - return [[self class] formSectionWithTitle:title sectionOptions:sectionOptions sectionInsertMode:XLFormSectionInsertModeLastRow]; + return [self formSectionWithTitle:title sectionOptions:sectionOptions sectionInsertMode:XLFormSectionInsertModeLastRow]; } +(instancetype)formSectionWithTitle:(NSString *)title sectionOptions:(XLFormSectionOptions)sectionOptions sectionInsertMode:(XLFormSectionInsertMode)sectionInsertMode { - return [[[self class] alloc] initWithTitle:title sectionOptions:sectionOptions sectionInsertMode:sectionInsertMode]; + return [[XLFormSectionDescriptor alloc] initWithTitle:title sectionOptions:sectionOptions sectionInsertMode:sectionInsertMode]; } -(BOOL)isMultivaluedSection diff --git a/XLForm/XL/XLForm.h b/XLForm/XL/XLForm.h index 9ef59057..81874bce 100755 --- a/XLForm/XL/XLForm.h +++ b/XLForm/XL/XLForm.h @@ -105,6 +105,7 @@ extern NSString *const XLFormRowDescriptorTypeBooleanSwitch; extern NSString *const XLFormRowDescriptorTypeButton; extern NSString *const XLFormRowDescriptorTypeInfo; extern NSString *const XLFormRowDescriptorTypeStepCounter; +extern NSString *const XLFormRowDescriptorTypeCreditCardExpiryDate; #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) diff --git a/XLForm/XL/XLForm.m b/XLForm/XL/XLForm.m index 49336645..322e019d 100644 --- a/XLForm/XL/XLForm.m +++ b/XLForm/XL/XLForm.m @@ -64,4 +64,4 @@ NSString *const XLFormRowDescriptorTypeButton = @"button"; NSString *const XLFormRowDescriptorTypeInfo = @"info"; NSString *const XLFormRowDescriptorTypeStepCounter = @"stepCounter"; - +NSString *const XLFormRowDescriptorTypeCreditCardExpiryDate = @"expiry";