Skip to content

Commit

Permalink
New flat buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
weiran committed Aug 23, 2013
1 parent 6e46db9 commit 7881e98
Showing 1 changed file with 26 additions and 95 deletions.
121 changes: 26 additions & 95 deletions Hackers/WZButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,111 +8,42 @@

#import "WZButton.h"

@interface WZButton ()
@property (nonatomic, strong) UIColor *backgroundColor;
@property (nonatomic, strong) UIColor *mainColor;
@end

@implementation WZButton

- (void)awakeFromNib {
[self setTitleColor:[WZTheme titleTextColor] forState:UIControlStateNormal];
[self setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont fontWithName:kBodyFontName size:kBodyFontSize];
}

- (void)drawRect:(CGRect)rect {
if ([WZTheme lightTheme]) {
[self drawLightTheme];
} else {
[self drawDarkTheme];
}
}

- (void)drawDarkTheme {
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* baseGradientBottomColor = [UIColor colorWithRed: 0.122 green: 0.122 blue: 0.122 alpha: 1];
UIColor* buttonColor = [UIColor colorWithRed: 0.184 green: 0.184 blue: 0.184 alpha: 1];

//// Gradient Declarations
NSArray* baseGradientColors = [NSArray arrayWithObjects:
(id)buttonColor.CGColor,
(id)baseGradientBottomColor.CGColor, nil];
CGFloat baseGradientLocations[] = {0, 1};
CGGradientRef baseGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)baseGradientColors, baseGradientLocations);
CGColorSpaceRelease(colorSpace);

//// Frames
CGRect frame = self.bounds;
self.backgroundColor = [UIColor clearColor];
self.mainColor = [WZTheme segmentBackgroundColor];

[self setTitleColor:self.mainColor forState:UIControlStateNormal];
[self setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];

//// Button
{
//// ButtonRectangle Drawing
CGRect buttonRectangleRect = CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 1, CGRectGetWidth(frame) - 4, CGRectGetHeight(frame) - 3);
UIBezierPath* buttonRectanglePath = [UIBezierPath bezierPathWithRoundedRect: buttonRectangleRect cornerRadius: 3];
CGContextSaveGState(context);
CGContextBeginTransparencyLayer(context, NULL);
[buttonRectanglePath addClip];
CGContextDrawLinearGradient(context, baseGradient,
CGPointMake(CGRectGetMidX(buttonRectangleRect), CGRectGetMinY(buttonRectangleRect)),
CGPointMake(CGRectGetMidX(buttonRectangleRect), CGRectGetMaxY(buttonRectangleRect)),
0);
CGContextEndTransparencyLayer(context);
CGContextRestoreGState(context);
CGGradientRelease(baseGradient);
}
self.titleLabel.font = [UIFont fontWithName:kBodyFontName size:kBodyFontSize];
}

- (void)drawLightTheme {
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* baseGradientBottomColor = [UIColor colorWithRed: 0.945 green: 0.945 blue: 0.945 alpha: 1];
UIColor* buttonColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];
UIColor* iconShadow = [UIColor colorWithRed: 0.2 green: 0.2 blue: 0.2 alpha: 1];

//// Gradient Declarations
NSArray* baseGradientColors = [NSArray arrayWithObjects:
(id)buttonColor.CGColor,
(id)baseGradientBottomColor.CGColor, nil];
CGFloat baseGradientLocations[] = {0, 1};
CGGradientRef baseGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)baseGradientColors, baseGradientLocations);

//// Shadow Declarations
UIColor* buttonShadow = iconShadow;
CGSize buttonShadowOffset = CGSizeMake(0.1, -0.1);
CGFloat buttonShadowBlurRadius = 2;

//// Frames
CGRect frame = self.bounds;


//// Button
{
//// ButtonRectangle Drawing
CGRect buttonRectangleRect = CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 1, CGRectGetWidth(frame) - 4, CGRectGetHeight(frame) - 3);
UIBezierPath* buttonRectanglePath = [UIBezierPath bezierPathWithRoundedRect: buttonRectangleRect cornerRadius: 3];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, buttonShadowOffset, buttonShadowBlurRadius, buttonShadow.CGColor);
CGContextBeginTransparencyLayer(context, NULL);
[buttonRectanglePath addClip];
CGContextDrawLinearGradient(context, baseGradient,
CGPointMake(CGRectGetMidX(buttonRectangleRect), CGRectGetMinY(buttonRectangleRect)),
CGPointMake(CGRectGetMidX(buttonRectangleRect), CGRectGetMaxY(buttonRectangleRect)),
0);
CGContextEndTransparencyLayer(context);
CGContextRestoreGState(context);
- (void)setHighlighted:(BOOL)highlighted {
if (self.highlighted != highlighted) {
[super setHighlighted:highlighted];

UIColor *temp = self.mainColor;
self.mainColor = self.backgroundColor;
self.backgroundColor = temp;

[self setNeedsDisplay];
}


//// Cleanup
CGGradientRelease(baseGradient);
CGColorSpaceRelease(colorSpace);

}

- (void)drawRect:(CGRect)rect {
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectInset(rect, 1, 1) cornerRadius: 5];
[self.backgroundColor setFill];
[roundedRectanglePath fill];
[self.mainColor setStroke];
roundedRectanglePath.lineWidth = 2;
[roundedRectanglePath stroke];
}

@end

0 comments on commit 7881e98

Please sign in to comment.