From dadc118cfcd80ceab3ced530e2398658e6ddeaee Mon Sep 17 00:00:00 2001 From: Ben Nicholas Date: Fri, 8 Aug 2014 13:06:11 -0400 Subject: [PATCH 1/3] Simplify color scheme to use tint color MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rather than having an internal color scheme which can’t be easily inherited, move to using the tintColor of the VENTokenField to control the color of tokens. This allows just as much customizability, while allowing common color schemes in an application to carry over without custom color assignments. For example, changing the global tint color will now be inherited by the token field if changed in either the storyboard or by setting the UIWindow’s tint color. As a side effect, tint adjustment modes (fading colors when an alert view is displayed) is supported. --- VENTokenField/VENToken.h | 1 - VENTokenField/VENToken.m | 14 ++++++-------- VENTokenField/VENTokenField.h | 2 -- VENTokenField/VENTokenField.m | 20 +++++++------------- VENTokenFieldSample/ViewController.m | 3 ++- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 21768cb..84d3808 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -26,7 +26,6 @@ @property (assign, nonatomic) BOOL highlighted; @property (copy, nonatomic) void (^didTapTokenBlock) (void); -@property (strong, nonatomic) UIColor *colorScheme; - (void)setTitleText:(NSString *)text; diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index 4435a83..df0589e 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -43,15 +43,14 @@ - (void)setUpInit { self.backgroundView.layer.cornerRadius = 5; self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapToken:)]; - self.colorScheme = [UIColor blueColor]; - self.titleLabel.textColor = self.colorScheme; + self.titleLabel.textColor = self.tintColor; [self addGestureRecognizer:self.tapGestureRecognizer]; } - (void)setTitleText:(NSString *)text { self.titleLabel.text = text; - self.titleLabel.textColor = self.colorScheme; + self.titleLabel.textColor = self.tintColor; [self.titleLabel sizeToFit]; self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), CGRectGetMaxX(self.titleLabel.frame) + 3, CGRectGetHeight(self.frame)); [self.titleLabel sizeToFit]; @@ -60,16 +59,15 @@ - (void)setTitleText:(NSString *)text - (void)setHighlighted:(BOOL)highlighted { _highlighted = highlighted; - UIColor *textColor = highlighted ? [UIColor whiteColor] : self.colorScheme; - UIColor *backgroundColor = highlighted ? self.colorScheme : [UIColor clearColor]; + UIColor *textColor = highlighted ? [UIColor whiteColor] : self.tintColor; + UIColor *backgroundColor = highlighted ? self.tintColor : [UIColor clearColor]; self.titleLabel.textColor = textColor; self.backgroundView.backgroundColor = backgroundColor; } -- (void)setColorScheme:(UIColor *)colorScheme +- (void)tintColorDidChange { - _colorScheme = colorScheme; - self.titleLabel.textColor = self.colorScheme; + self.titleLabel.textColor = self.tintColor; [self setHighlighted:_highlighted]; } diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index 5d5557e..f4f94e2 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -68,7 +68,5 @@ @property (copy, nonatomic) NSString *placeholderText; -- (void)setColorScheme:(UIColor *)color; - @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index dc3cba4..25f186f 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -42,7 +42,6 @@ @interface VENTokenField () @property (strong, nonatomic) UITapGestureRecognizer *tapGestureRecognizer; @property (strong, nonatomic) VENBackspaceTextField *invisibleTextField; @property (strong, nonatomic) VENBackspaceTextField *inputTextField; -@property (strong, nonatomic) UIColor *colorScheme; @property (strong, nonatomic) UILabel *collapsedLabel; @end @@ -84,7 +83,6 @@ - (void)setUpInit self.horizontalInset = VENTokenFieldDefaultHorizontalInset; self.tokenPadding = VENTokenFieldDefaultTokenPadding; self.minInputWidth = VENTokenFieldDefaultMinInputWidth; - self.colorScheme = [UIColor blueColor]; self.toLabelTextColor = [UIColor colorWithRed:112/255.0f green:124/255.0f blue:124/255.0f alpha:1.0f]; self.inputTextFieldTextColor = [UIColor colorWithRed:38/255.0f green:39/255.0f blue:41/255.0f alpha:1.0f]; @@ -161,14 +159,10 @@ - (void)setToLabelTextColor:(UIColor *)toLabelTextColor self.toLabel.textColor = _toLabelTextColor; } -- (void)setColorScheme:(UIColor *)color +- (void)tintColorDidChange { - _colorScheme = color; - self.collapsedLabel.textColor = color; - self.inputTextField.tintColor = color; - for (VENToken *token in self.tokens) { - [token setColorScheme:color]; - } + self.collapsedLabel.textColor = self.tintColor; + self.inputTextField.tintColor = self.tintColor; } - (NSString *)inputText @@ -203,7 +197,7 @@ - (void)layoutInputTextFieldWithCurrentX:(CGFloat *)currentX currentY:(CGFloat * VENBackspaceTextField *inputTextField = self.inputTextField; inputTextField.text = @""; inputTextField.frame = CGRectMake(*currentX, *currentY + 1, inputTextFieldWidth, [self heightForToken] - 1); - inputTextField.tintColor = self.colorScheme; + inputTextField.tintColor = self.tintColor; [self.scrollView addSubview:inputTextField]; } @@ -212,7 +206,7 @@ - (void)layoutCollapsedLabelWithCurrentX:(CGFloat *)currentX UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(*currentX, CGRectGetMinY(self.toLabel.frame), self.width - *currentX - self.horizontalInset, self.toLabel.height)]; label.font = [UIFont fontWithName:@"HelveticaNeue" size:15.5]; label.text = [self collapsedText]; - label.textColor = self.colorScheme; + label.textColor = self.tintColor; label.minimumScaleFactor = 5./label.font.pointSize; label.adjustsFontSizeToFitWidth = YES; [self addSubview:label]; @@ -233,7 +227,7 @@ - (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; + token.tintColor = self.tintColor; __weak VENToken *weakToken = token; token.didTapTokenBlock = ^{ @@ -326,7 +320,7 @@ - (VENBackspaceTextField *)inputTextField _inputTextField.font = [UIFont fontWithName:@"HelveticaNeue" size:15.5]; _inputTextField.accessibilityLabel = NSLocalizedString(@"To", nil); _inputTextField.autocorrectionType = UITextAutocorrectionTypeNo; - _inputTextField.tintColor = self.colorScheme; + _inputTextField.tintColor = self.tintColor; _inputTextField.delegate = self; _inputTextField.placeholder = self.placeholderText; [_inputTextField addTarget:self action:@selector(inputTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; diff --git a/VENTokenFieldSample/ViewController.m b/VENTokenFieldSample/ViewController.m index b50205d..b25d9b0 100644 --- a/VENTokenFieldSample/ViewController.m +++ b/VENTokenFieldSample/ViewController.m @@ -23,7 +23,8 @@ - (void)viewDidLoad self.tokenField.delegate = self; self.tokenField.dataSource = self; self.tokenField.placeholderText = NSLocalizedString(@"Enter names here", nil); - [self.tokenField setColorScheme:[UIColor colorWithRed:61/255.0f green:149/255.0f blue:206/255.0f alpha:1.0f]]; + [self.tokenField setTintColor:[UIColor colorWithRed:61/255.0f green:149/255.0f blue:206/255.0f alpha:1.0f]]; + self.tokenField.tint [self.tokenField becomeFirstResponder]; } From 07eb915d5886e2870e9d12efbddf8e872c7cec79 Mon Sep 17 00:00:00 2001 From: Ben Nicholas Date: Fri, 8 Aug 2014 14:46:51 -0400 Subject: [PATCH 2/3] Whoops, had a focus issue that got committed accidentally... --- VENTokenFieldSample/ViewController.m | 1 - 1 file changed, 1 deletion(-) diff --git a/VENTokenFieldSample/ViewController.m b/VENTokenFieldSample/ViewController.m index b25d9b0..6933575 100644 --- a/VENTokenFieldSample/ViewController.m +++ b/VENTokenFieldSample/ViewController.m @@ -24,7 +24,6 @@ - (void)viewDidLoad self.tokenField.dataSource = self; self.tokenField.placeholderText = NSLocalizedString(@"Enter names here", nil); [self.tokenField setTintColor:[UIColor colorWithRed:61/255.0f green:149/255.0f blue:206/255.0f alpha:1.0f]]; - self.tokenField.tint [self.tokenField becomeFirstResponder]; } From 3a6bcb9e3b16457fa686a4171543e666e3c1e0b0 Mon Sep 17 00:00:00 2001 From: Ben Nicholas Date: Mon, 11 Aug 2014 10:07:41 -0400 Subject: [PATCH 3/3] Add back color scheme to external API To prevent a major release (and breaking backwards compatibility) mark color scheme as deprecated but proxy it forward to tint color internally. --- VENTokenField/VENToken.h | 1 + VENTokenField/VENToken.m | 9 +++++++++ VENTokenField/VENTokenField.h | 2 ++ VENTokenField/VENTokenField.m | 5 +++++ 4 files changed, 17 insertions(+) diff --git a/VENTokenField/VENToken.h b/VENTokenField/VENToken.h index 84d3808..a6608bd 100644 --- a/VENTokenField/VENToken.h +++ b/VENTokenField/VENToken.h @@ -26,6 +26,7 @@ @property (assign, nonatomic) BOOL highlighted; @property (copy, nonatomic) void (^didTapTokenBlock) (void); +@property (strong, nonatomic) UIColor *colorScheme DEPRECATED_ATTRIBUTE; - (void)setTitleText:(NSString *)text; diff --git a/VENTokenField/VENToken.m b/VENTokenField/VENToken.m index df0589e..102b733 100644 --- a/VENTokenField/VENToken.m +++ b/VENTokenField/VENToken.m @@ -71,6 +71,15 @@ - (void)tintColorDidChange [self setHighlighted:_highlighted]; } +// proxy color scheme out to tint color to avoid a major release. +- (UIColor *)colorScheme { + return self.tintColor; +} + +- (void)setColorScheme:(UIColor *)colorScheme +{ + self.tintColor = colorScheme; +} #pragma mark - Private diff --git a/VENTokenField/VENTokenField.h b/VENTokenField/VENTokenField.h index f4f94e2..0da7f41 100644 --- a/VENTokenField/VENTokenField.h +++ b/VENTokenField/VENTokenField.h @@ -68,5 +68,7 @@ @property (copy, nonatomic) NSString *placeholderText; +- (void)setColorScheme:(UIColor *)color DEPRECATED_ATTRIBUTE; + @end diff --git a/VENTokenField/VENTokenField.m b/VENTokenField/VENTokenField.m index 25f186f..952bc9f 100644 --- a/VENTokenField/VENTokenField.m +++ b/VENTokenField/VENTokenField.m @@ -165,6 +165,11 @@ - (void)tintColorDidChange self.inputTextField.tintColor = self.tintColor; } +- (void)setColorScheme:(UIColor *)color +{ + self.tintColor = color; +} + - (NSString *)inputText { return self.inputTextField.text;