From 62368c2a6d149927d3c9a153865e52ab83707609 Mon Sep 17 00:00:00 2001 From: Sam Soffes Date: Mon, 29 Jul 2013 17:01:09 -0400 Subject: [PATCH] Add `useThinGradients` and fix running on iOS 6 when compiled with iOS 7. --- SAMGradientView/SAMGradientView.h | 7 +++++++ SAMGradientView/SAMGradientView.m | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/SAMGradientView/SAMGradientView.h b/SAMGradientView/SAMGradientView.h index 7b56363..498994a 100644 --- a/SAMGradientView/SAMGradientView.h +++ b/SAMGradientView/SAMGradientView.h @@ -71,6 +71,13 @@ typedef enum : NSUInteger { /// @name Drawing the Borders ///-------------------------- +/** + Use thin borders. + + 1px borders will be drawn instead of 1pt borders. The default is `NO`. + */ +@property (nonatomic) BOOL useThinBorders; + /** The top border color. The default is `nil`. diff --git a/SAMGradientView/SAMGradientView.m b/SAMGradientView/SAMGradientView.m index d4ea6c0..b0b34b1 100644 --- a/SAMGradientView/SAMGradientView.m +++ b/SAMGradientView/SAMGradientView.m @@ -20,6 +20,7 @@ @implementation SAMGradientView @synthesize gradientColors = _gradientColors; @synthesize gradientLocations = _gradientLocations; @synthesize gradientDirection = _gradientDirection; +@synthesize useThinBorders = _useThinBorders; @synthesize topBorderColor = _topBorderColor; @synthesize topInsetColor = _topInsetColor; @synthesize rightBorderColor = _rightBorderColor; @@ -84,6 +85,12 @@ - (void)setDimmedGradientColors:(NSArray *)colors { #endif +- (void)setUseThinBorders:(BOOL)useThinBorders { + _useThinBorders = useThinBorders; + [self setNeedsDisplay]; +} + + - (void)setTopBorderColor:(UIColor *)topBorderColor { _topBorderColor = topBorderColor; [self setNeedsDisplay]; @@ -161,7 +168,7 @@ - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGSize size = self.bounds.size; - CGFloat const borderWidth = 1.0f; + CGFloat borderWidth = self.useThinBorders ? 1.0f / [[UIScreen mainScreen] scale] : 1.0f; // Gradient if (self.gradient) { @@ -184,14 +191,14 @@ - (void)drawRect:(CGRect)rect { } } - CGFloat sideY = self.topBorderColor ? 1.0f : 0.0f; + CGFloat sideY = self.topBorderColor ? borderWidth : 0.0f; CGFloat sideHeight = size.height; if (self.topBorderColor) { - sideHeight -= 1.0f; + sideHeight -= borderWidth; } if (self.bottomBorderColor) { - sideHeight -= 1.0f; + sideHeight -= borderWidth; } // Right @@ -252,7 +259,7 @@ - (void)initialize { - (void)refreshGradient { #ifdef __IPHONE_7_0 - if (self.tintAdjustmentMode == UIViewTintAdjustmentModeDimmed) { + if ([self respondsToSelector:@selector(tintAdjustmentMode)] && self.tintAdjustmentMode == UIViewTintAdjustmentModeDimmed) { NSArray *locations = self.dimmedGradientColors.count == self.gradientLocations.count ? self.gradientLocations : nil; self.gradient = SAMGradientCreateWithColorsAndLocations(self.dimmedGradientColors, locations); return;