Skip to content

Commit

Permalink
Merge pull request #57 from MaheshRS/master
Browse files Browse the repository at this point in the history
Flexibility to choose color for individual texts in VENTokenField
  • Loading branch information
ayanonagon committed Apr 10, 2015
2 parents 4566a2f + b5eb460 commit 1155ea9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions VENTokenField/VENTokenField.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index;
- (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField;
- (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField;
- (UIColor *)tokenField:(VENTokenField *)tokenField colorSchemeForTokenAtIndex:(NSUInteger)index;
@end


Expand Down
17 changes: 16 additions & 1 deletion VENTokenField/VENTokenField.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current
for (NSUInteger i = 0; i < [self numberOfTokens]; i++) {
NSString *title = [self titleForTokenAtIndex:i];
VENToken *token = [[VENToken alloc] init];
token.colorScheme = self.colorScheme;

__weak VENToken *weakToken = token;
__weak VENTokenField *weakSelf = self;
Expand All @@ -307,6 +306,8 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current
};

[token setTitleText:[NSString stringWithFormat:@"%@,", title]];
token.colorScheme = [self colorSchemeForTokenAtIndex:i];

[self.tokens addObject:token];

if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line
Expand Down Expand Up @@ -455,6 +456,7 @@ - (void)unhighlightAllTokens
for (VENToken *token in self.tokens) {
token.highlighted = NO;
}

[self setCursorVisibility];
}

Expand All @@ -463,6 +465,7 @@ - (void)setCursorVisibility
NSArray *highlightedTokens = [self.tokens filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(VENToken *evaluatedObject, NSDictionary *bindings) {
return evaluatedObject.highlighted;
}]];

BOOL visible = [highlightedTokens count] == 0;
if (visible) {
[self inputTextFieldBecomeFirstResponder];
Expand All @@ -485,6 +488,14 @@ - (void)focusInputTextField
}
}

- (UIColor *)colorSchemeForTokenAtIndex:(NSUInteger)index {

if ([self.dataSource respondsToSelector:@selector(tokenField:colorSchemeForTokenAtIndex:)]) {
return [self.dataSource tokenField:self colorSchemeForTokenAtIndex:index];
}

return self.colorScheme;
}

#pragma mark - Data Source

Expand All @@ -493,6 +504,7 @@ - (NSString *)titleForTokenAtIndex:(NSUInteger)index
if ([self.dataSource respondsToSelector:@selector(tokenField:titleForTokenAtIndex:)]) {
return [self.dataSource tokenField:self titleForTokenAtIndex:index];
}

return [NSString string];
}

Expand All @@ -501,6 +513,7 @@ - (NSUInteger)numberOfTokens
if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) {
return [self.dataSource numberOfTokensInTokenField:self];
}

return 0;
}

Expand All @@ -509,6 +522,7 @@ - (NSString *)collapsedText
if ([self.dataSource respondsToSelector:@selector(tokenFieldCollapsedText:)]) {
return [self.dataSource tokenFieldCollapsedText:self];
}

return @"";
}

Expand All @@ -522,6 +536,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
[self.delegate tokenField:self didEnterText:textField.text];
}
}

return NO;
}

Expand Down

0 comments on commit 1155ea9

Please sign in to comment.