From f5bc6800926bc5e101165fbd75349556c12e1076 Mon Sep 17 00:00:00 2001 From: Mahesh Shanbhag Date: Thu, 19 Mar 2015 13:14:50 +0530 Subject: [PATCH 1/2] Flexibility to choose color for individual texts in VENTokenField --- VENTokenField/VENToken.h | 1 + VENTokenField/VENToken.m | 4 ++++ VENTokenField/VENTokenField.h | 1 + VENTokenField/VENTokenField.m | 17 ++++++++++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 21768cb..8c6d41e 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -27,6 +27,7 @@ @property (assign, nonatomic) BOOL highlighted; @property (copy, nonatomic) void (^didTapTokenBlock) (void); @property (strong, nonatomic) UIColor *colorScheme; +@property (strong, nonatomic, readonly) NSString *title; - (void)setTitleText:(NSString *)text; diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 4435a83..fed190f 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -73,6 +73,10 @@ - (void)setColorScheme:(UIColor *)colorScheme [self setHighlighted:_highlighted]; } +- (NSString *)title { + return self.titleLabel.text; +} + #pragma mark - Private diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index b4c81c4..2f90edd 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -36,6 +36,7 @@ - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index; - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField; - (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField; +- (UIColor *)colorSchemeForText:(NSString *)text inTokenField:(VENTokenField *)field; @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 3512062..1dbd4c8 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -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; @@ -307,6 +306,8 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current }; [token setTitleText:[NSString stringWithFormat:@"%@,", title]]; + token.colorScheme = [self colorSchemeForToken:token]; + [self.tokens addObject:token]; if (*currentX + token.width <= self.scrollView.contentSize.width) { // token fits in current line @@ -455,6 +456,7 @@ - (void)unhighlightAllTokens for (VENToken *token in self.tokens) { token.highlighted = NO; } + [self setCursorVisibility]; } @@ -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]; @@ -485,6 +488,14 @@ - (void)focusInputTextField } } +- (UIColor *)colorSchemeForToken:(VENToken *)token { + + if ([self.dataSource respondsToSelector:@selector(colorSchemeForText:inTokenField:)]) { + return [self.dataSource colorSchemeForText:token.title inTokenField:self]; + } + + return self.colorScheme; +} #pragma mark - Data Source @@ -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]; } @@ -501,6 +513,7 @@ - (NSUInteger)numberOfTokens if ([self.dataSource respondsToSelector:@selector(numberOfTokensInTokenField:)]) { return [self.dataSource numberOfTokensInTokenField:self]; } + return 0; } @@ -509,6 +522,7 @@ - (NSString *)collapsedText if ([self.dataSource respondsToSelector:@selector(tokenFieldCollapsedText:)]) { return [self.dataSource tokenFieldCollapsedText:self]; } + return @""; } @@ -522,6 +536,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField [self.delegate tokenField:self didEnterText:textField.text]; } } + return NO; } From b5eb46053906bfee38ec3b108d8fef02f610d9fd Mon Sep 17 00:00:00 2001 From: Mahesh Shanbhag Date: Wed, 25 Mar 2015 20:32:07 +0530 Subject: [PATCH 2/2] Using index instead of token title for color scheme of individual token. --- VENTokenField/VENToken.h | 1 - VENTokenField/VENToken.m | 4 ---- VENTokenField/VENTokenField.h | 2 +- VENTokenField/VENTokenField.m | 8 ++++---- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 8c6d41e..21768cb 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -27,7 +27,6 @@ @property (assign, nonatomic) BOOL highlighted; @property (copy, nonatomic) void (^didTapTokenBlock) (void); @property (strong, nonatomic) UIColor *colorScheme; -@property (strong, nonatomic, readonly) NSString *title; - (void)setTitleText:(NSString *)text; diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index fed190f..4435a83 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -73,10 +73,6 @@ - (void)setColorScheme:(UIColor *)colorScheme [self setHighlighted:_highlighted]; } -- (NSString *)title { - return self.titleLabel.text; -} - #pragma mark - Private diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index 2f90edd..73ea5dd 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -36,7 +36,7 @@ - (NSString *)tokenField:(VENTokenField *)tokenField titleForTokenAtIndex:(NSUInteger)index; - (NSUInteger)numberOfTokensInTokenField:(VENTokenField *)tokenField; - (NSString *)tokenFieldCollapsedText:(VENTokenField *)tokenField; -- (UIColor *)colorSchemeForText:(NSString *)text inTokenField:(VENTokenField *)field; +- (UIColor *)tokenField:(VENTokenField *)tokenField colorSchemeForTokenAtIndex:(NSUInteger)index; @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 1dbd4c8..0f54c74 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -306,7 +306,7 @@ - (void)layoutTokensWithCurrentX:(CGFloat *)currentX currentY:(CGFloat *)current }; [token setTitleText:[NSString stringWithFormat:@"%@,", title]]; - token.colorScheme = [self colorSchemeForToken:token]; + token.colorScheme = [self colorSchemeForTokenAtIndex:i]; [self.tokens addObject:token]; @@ -488,10 +488,10 @@ - (void)focusInputTextField } } -- (UIColor *)colorSchemeForToken:(VENToken *)token { +- (UIColor *)colorSchemeForTokenAtIndex:(NSUInteger)index { - if ([self.dataSource respondsToSelector:@selector(colorSchemeForText:inTokenField:)]) { - return [self.dataSource colorSchemeForText:token.title inTokenField:self]; + if ([self.dataSource respondsToSelector:@selector(tokenField:colorSchemeForTokenAtIndex:)]) { + return [self.dataSource tokenField:self colorSchemeForTokenAtIndex:index]; } return self.colorScheme;