diff --git a/TITokenField.h b/TITokenField.h index 6bc1ec4..acf3791 100644 --- a/TITokenField.h +++ b/TITokenField.h @@ -88,6 +88,7 @@ typedef enum { @property (nonatomic, weak) id delegate; @property (weak, nonatomic, readonly) NSArray * tokens; @property (weak, nonatomic, readonly) TIToken * selectedToken; +@property (weak, nonatomic, readonly) TIToken * highlightedToken; @property (weak, nonatomic, readonly) NSArray * tokenTitles; @property (weak, nonatomic, readonly) NSArray * tokenObjects; @property (nonatomic, assign) BOOL editable; @@ -135,6 +136,7 @@ typedef enum { @property (nonatomic, strong) UIColor * tintColor UI_APPEARANCE_SELECTOR; @property (nonatomic, assign) TITokenAccessoryType accessoryType; @property (nonatomic, assign) CGFloat maxWidth; +@property (nonatomic, assign) BOOL flatDesign; - (instancetype)initWithTitle:(NSString *)aTitle; - (instancetype)initWithTitle:(NSString *)aTitle representedObject:(id)object; diff --git a/TITokenField.m b/TITokenField.m index 15f5c50..6ee10bf 100644 --- a/TITokenField.m +++ b/TITokenField.m @@ -764,10 +764,16 @@ - (void)tokenTouchDown:(TIToken *)token { [_selectedToken setSelected:NO]; _selectedToken = nil; } + + if (_highlightedToken != token) { + _highlightedToken = nil; + } + _highlightedToken = token; } - (void)tokenTouchUpInside:(TIToken *)token { if (_editable) [self selectToken:token]; + _highlightedToken = nil; } - (CGFloat)layoutTokensInternal { @@ -1097,6 +1103,8 @@ - (instancetype)initWithTitle:(NSString *)aTitle representedObject:(id)object fo _accessoryType = TITokenAccessoryTypeNone; _maxWidth = 200; + _flatDesign = NO; + [self setBackgroundColor:[UIColor clearColor]]; [self sizeToFit]; } @@ -1237,28 +1245,56 @@ - (void)drawRect:(CGRect)rect { CGContextRestoreGState(context); CGPathRef innerPath = CGPathCreateTokenPath(self.bounds.size, YES); - - // Draw a white background so we can use alpha to lighten the inner gradient - CGContextSaveGState(context); - CGContextAddPath(context, innerPath); - CGContextSetFillColor(context, (CGFloat[4]){1, 1, 1, 1}); - CGContextFillPath(context); - CGContextRestoreGState(context); - - // Draw the inner gradient. - CGContextSaveGState(context); - CGContextAddPath(context, innerPath); - CGPathRelease(innerPath); - CGContextClip(context); + CGFloat highlightedComp[8]; - CGFloat locations[2] = {0, (drawHighlighted ? 0.9 : 0.6)}; - CGFloat highlightedComp[8] = {red, green, blue, 0.7, red, green, blue, 1}; - CGFloat nonHighlightedComp[8] = {red, green, blue, 0.15, red, green, blue, 0.3}; - - CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, (drawHighlighted ? highlightedComp : nonHighlightedComp), locations, 2); - CGContextDrawLinearGradient(context, gradient, CGPointZero, endPoint, 0); - CGGradientRelease(gradient); - CGContextRestoreGState(context); + if (self.flatDesign) { + // Flat design + CGContextSaveGState(context); + CGContextAddPath(context, innerPath); + + if (drawHighlighted && [self isColorDarker:self.tintColor]) { + CGContextSetFillColor(context, CGColorGetComponents([self lighterColorForColor:self.tintColor].CGColor)); + } else if (drawHighlighted && ![self isColorDarker:self.tintColor]) { + CGContextSetFillColor(context, CGColorGetComponents([self darkerColorForColor:self.tintColor].CGColor)); + } else { + CGContextSetFillColor(context, CGColorGetComponents(self.tintColor.CGColor)); + } + + + CGContextFillPath(context); + CGContextRestoreGState(context); + } else { + + // Draw a white background so we can use alpha to lighten the inner gradient + CGContextSaveGState(context); + CGContextAddPath(context, innerPath); + CGContextSetFillColor(context, (CGFloat[4]){1, 1, 1, 1}); + CGContextFillPath(context); + CGContextRestoreGState(context); + + // Draw the inner gradient. + CGContextSaveGState(context); + CGContextAddPath(context, innerPath); + CGPathRelease(innerPath); + CGContextClip(context); + + CGFloat locations[2] = {0, (drawHighlighted ? 0.9 : 0.6)}; + highlightedComp[0] = red; + highlightedComp[1] = green; + highlightedComp[2] = blue; + highlightedComp[3] = 0.15; + highlightedComp[4] = red; + highlightedComp[5] = green; + highlightedComp[6] = blue; + highlightedComp[7] = 0.3; + CGFloat nonHighlightedComp[8] = {red, green, blue, 0.15, red, green, blue, 0.3}; + + CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, (drawHighlighted ? highlightedComp : nonHighlightedComp), locations, 2); + CGContextDrawLinearGradient(context, gradient, CGPointZero, endPoint, 0); + CGGradientRelease(gradient); + CGContextRestoreGState(context); + + } CGFloat accessoryWidth = 0; @@ -1284,9 +1320,11 @@ - (void)drawRect:(CGRect)rect { CGContextAddPath(context, disclosurePath); CGContextClip(context); - CGGradientRef disclosureGradient = CGGradientCreateWithColorComponents(colorspace, highlightedComp, NULL, 2); - CGContextDrawLinearGradient(context, disclosureGradient, CGPointZero, endPoint, 0); - CGGradientRelease(disclosureGradient); + if (!self.flatDesign) { + CGGradientRef disclosureGradient = CGGradientCreateWithColorComponents(colorspace, highlightedComp, NULL, 2); + CGContextDrawLinearGradient(context, disclosureGradient, CGPointZero, endPoint, 0); + CGGradientRelease(disclosureGradient); + } arrowPoint.y += 0.5; CGPathRef innerShadowPath = CGPathCreateDisclosureIndicatorPath(arrowPoint, _font.pointSize, kDisclosureThickness, NULL); @@ -1368,6 +1406,37 @@ - (BOOL)getTintColorRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)bl return NO; } +- (UIColor *)lighterColorForColor:(UIColor *)c +{ + CGFloat r, g, b, a; + if ([c getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MIN(r + 0.1, 1.0) + green:MIN(g + 0.1, 1.0) + blue:MIN(b + 0.1, 1.0) + alpha:a]; + return nil; +} + +- (UIColor *)darkerColorForColor:(UIColor *)c +{ + CGFloat r, g, b, a; + if ([c getRed:&r green:&g blue:&b alpha:&a]) + return [UIColor colorWithRed:MAX(r - 0.1, 0.0) + green:MAX(g - 0.1, 0.0) + blue:MAX(b - 0.1, 0.0) + alpha:a]; + return nil; +} + +- (BOOL)isColorDarker:(UIColor *)color { + // Calculates a weighted distance brightness + // brightness = sqrt( .241 R^2 + .691 G^2 + .068 B^2 ) + CGFloat * components = CGColorGetComponents(color.CGColor); + return sqrt(.241 * pow(components[0],2) + + .691 * pow(components[1],2) + + .068 * pow(components[3],2))>127.5?YES:NO; +} + #pragma mark Other - (NSString *)description { return [NSString stringWithFormat:@"", self, _title, _representedObject]; diff --git a/TITokenField.podspec b/TITokenField.podspec new file mode 100644 index 0000000..031baae --- /dev/null +++ b/TITokenField.podspec @@ -0,0 +1,28 @@ +Pod::Spec.new do |s| + s.name = "TITokenField" + s.version = "0.9.5" + s.summary = "An iOS version of the NSTokenField (See To: field in Mail and Messages)." + s.homepage = "https://github.com/thermogl/TITokenField" + s.license = { + :type => 'Dual-licensed (BSD and commercial)', + :text => <<-LICENSE + This control is dual licensed: You can use it for free under the BSD licence below or, If you require non-attribution you can purchase the commercial licence available at http://www.cocoacontrols.com/authors/thermogl + +Copyright (c) 2012 Tom Irving. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY TOM IRVING "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOM IRVING OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + LICENSE + } + + s.author = { "Tom Irving" => "info@thermoglobalnuclearwar.com" } + s.source = { :git => "https://github.com/thermogl/TITokenField.git", :tag => s.version.to_s } + s.platform = :ios, '5.0' + s.source_files = 'TITokenField.{h,m}' + s.requires_arc = true +end