Skip to content

Commit

Permalink
Merge pull request #68 from neerajbaid/nb/delimiters
Browse files Browse the repository at this point in the history
Custom delimiters.
  • Loading branch information
ayanonagon committed Jun 2, 2015
2 parents 4ff6232 + 73df463 commit e51abdc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions VENTokenField/VENTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

@property (strong, nonatomic) UILabel *toLabel;

@property (strong, nonatomic) NSArray *delimiters;
@property (copy, nonatomic) NSString *placeholderText;
@property (copy, nonatomic) NSString *inputTextFieldAccessibilityLabel;

Expand Down
13 changes: 13 additions & 0 deletions VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,19 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self unhighlightAllTokens];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
for (NSString *delimiter in self.delimiters) {
if (newString.length > delimiter.length &&
[[newString substringFromIndex:newString.length - delimiter.length] isEqualToString:delimiter]) {
NSString *enteredString = [newString substringToIndex:newString.length - delimiter.length];
if ([self.delegate respondsToSelector:@selector(tokenField:didEnterText:)]) {
if (enteredString.length) {
[self.delegate tokenField:self didEnterText:enteredString];
return NO;
}
}
}
}
return YES;
}

Expand Down
1 change: 1 addition & 0 deletions VENTokenFieldSample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (void)viewDidLoad
self.tokenField.placeholderText = NSLocalizedString(@"Enter names here", nil);
self.tokenField.toLabelText = NSLocalizedString(@"Post to:", nil);
[self.tokenField setColorScheme:[UIColor colorWithRed:61/255.0f green:149/255.0f blue:206/255.0f alpha:1.0f]];
self.tokenField.delimiters = @[@",", @";", @"--"];
[self.tokenField becomeFirstResponder];
}

Expand Down
35 changes: 32 additions & 3 deletions VENTokenFieldSampleTests/VENTokenFieldSampleTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,28 @@ - (void)testBasicFlow
[tester enterTextIntoCurrentFirstResponder:@"Octocat\n"];
[tester waitForViewWithAccessibilityLabel:@"Octocat,"];

[tester enterTextIntoCurrentFirstResponder:@"Steven,"];
[tester waitForViewWithAccessibilityLabel:@"Steven,"];

[tester enterTextIntoCurrentFirstResponder:@"Andy;"];
[tester waitForViewWithAccessibilityLabel:@"Andy,"];

[tester enterTextIntoCurrentFirstResponder:@"Sam--"];
[tester waitForViewWithAccessibilityLabel:@"Sam,"];

// Make sure everything else is still there.
[tester waitForViewWithAccessibilityLabel:@"Ayaka,"];
[tester waitForViewWithAccessibilityLabel:@"Mark,"];
[tester waitForViewWithAccessibilityLabel:@"Neeraj,"];
[tester waitForViewWithAccessibilityLabel:@"Octocat,"];
[tester waitForViewWithAccessibilityLabel:@"Steven,"];
[tester waitForViewWithAccessibilityLabel:@"Andy,"];
[tester waitForViewWithAccessibilityLabel:@"Sam,"];

// Delete
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester waitForAbsenceOfViewWithAccessibilityLabel:@"Octocat,"];
[tester waitForAbsenceOfViewWithAccessibilityLabel:@"Sam,"];

// Clear remaining names to reset state for next test
[tester tapViewWithAccessibilityLabel:@"To"];
Expand All @@ -45,6 +59,12 @@ - (void)testBasicFlow
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
[tester enterTextIntoCurrentFirstResponder:@"\b"];
}

- (void)testResignFirstResponder
Expand Down Expand Up @@ -81,12 +101,21 @@ - (void)testResignFirstResponderAndCollapse
[tester enterTextIntoCurrentFirstResponder:@"Octocat\n"];
[tester waitForViewWithAccessibilityLabel:@"Octocat,"];

[tester enterTextIntoCurrentFirstResponder:@"Steven,"];
[tester waitForViewWithAccessibilityLabel:@"Steven,"];

[tester enterTextIntoCurrentFirstResponder:@"Andy;"];
[tester waitForViewWithAccessibilityLabel:@"Andy,"];

[tester enterTextIntoCurrentFirstResponder:@"Sam--"];
[tester waitForViewWithAccessibilityLabel:@"Sam,"];

[tester tapViewWithAccessibilityLabel:@"Resign First Responder"];
[tester waitForAbsenceOfSoftwareKeyboard];
[tester tapViewWithAccessibilityLabel:@"Collapse token field"];

// Confirm the collapse label
[tester waitForViewWithAccessibilityLabel:@"4 people"];
[tester waitForViewWithAccessibilityLabel:@"7 people"];

// Remove one name and check again
[tester tapScreenAtPoint:tokenField.center];
Expand All @@ -96,7 +125,7 @@ - (void)testResignFirstResponderAndCollapse
[tester tapViewWithAccessibilityLabel:@"Resign First Responder"];
[tester waitForAbsenceOfSoftwareKeyboard];
[tester tapViewWithAccessibilityLabel:@"Collapse token field"];
[tester waitForViewWithAccessibilityLabel:@"3 people"];
[tester waitForViewWithAccessibilityLabel:@"6 people"];
}

@end

0 comments on commit e51abdc

Please sign in to comment.