Skip to content

Commit

Permalink
Support for multicharacter delimiters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeraj Baid committed May 13, 2015
1 parent c147289 commit ce1237f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,16 @@ - (void)textFieldDidBeginEditing:(UITextField *)textField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self unhighlightAllTokens];
if ([self.delimiters containsObject:string]) {
if ([self.delegate respondsToSelector:@selector(tokenField:didEnterText:)]) {
if ([textField.text length]) {
[self.delegate tokenField:self didEnterText:textField.text];
return NO;
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 = [textField.text substringToIndex:newString.length - delimiter.length];
if ([self.delegate respondsToSelector:@selector(tokenField:didEnterText:)]) {
if (enteredString.length) {
[self.delegate tokenField:self didEnterText:enteredString];
return NO;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion VENTokenFieldSample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +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.delimiters = @[@",", @";", @"--"];
[self.tokenField becomeFirstResponder];
}

Expand Down

0 comments on commit ce1237f

Please sign in to comment.