Skip to content

Instantly share code, notes, and snippets.

@geta6
Last active December 11, 2015 21:18
Show Gist options
  • Save geta6/4661301 to your computer and use it in GitHub Desktop.
Save geta6/4661301 to your computer and use it in GitHub Desktop.
enum {
UIButtonColorsTan,
UIButtonColorsBlack,
UIButtonColorsGreen,
UIButtonColorsOrange,
UIButtonColorsBlue,
UIButtonColorsWhite,
UIButtonColorsGrey
};
typedef NSUInteger UIButtonColors;
@interface UIButton(TcEx)
- (void)applyAppearance:(UIButtonColors)colors;
- (void)applyAppearance:(UIButtonColors)colors withFont:(UIFont *)font;
@end
@interface UIBarButtonItem(TcEx)
- (void)applyAppearance:(UIButtonColors)colors;
- (void)applyAppearance:(UIButtonColors)colors withSize:(CGFloat)width;
@end
#import "Test+Exntention.h"
#pragma mark - UIButton
@implementation UIButton(TcEx)
- (void)applyAppearance:(UIButtonColors)colors;
{
[self applyAppearance:colors withFont:[UIFont fontWithName:@"Helvetica Neue" size:15]];
}
- (void)applyAppearance:(UIButtonColors)colors withFont:(UIFont *)font
{
[self.titleLabel setFont:font];
NSString *usualImageName = @"";
NSString *hoverImageName = @"";
switch (colors) {
case UIButtonColorsBlack:
usualImageName = @"blackButton";
hoverImageName = @"blackButtonHighlight";
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor blackColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, -1)];
break;
case UIButtonColorsBlue:
usualImageName = @"blueButton";
hoverImageName = @"blueButtonHighlight";
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor blackColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, -1)];
break;
case UIButtonColorsGreen:
usualImageName = @"greenButton";
hoverImageName = @"greenButtonHighlight";
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor blackColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, -1)];
break;
case UIButtonColorsOrange:
usualImageName = @"orangeButton";
hoverImageName = @"orangeButtonHighlight";
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor blackColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, -1)];
break;
case UIButtonColorsTan:
usualImageName = @"tanButton";
hoverImageName = @"tanButtonHighlight";
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor whiteColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, 1)];
break;
case UIButtonColorsGrey:
usualImageName = @"GreyButton";
hoverImageName = @"GreyButtonHighlight";
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor whiteColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, 1)];
break;
case UIButtonColorsWhite:
usualImageName = @"whiteButton";
hoverImageName = @"whiteButtonHighlight";
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[self.titleLabel setShadowColor:[UIColor whiteColor]];
[self.titleLabel setShadowOffset:CGSizeMake(0, 1)];
break;
}
UIImage *usualImage = [[UIImage imageNamed:usualImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[self setBackgroundImage:usualImage forState:UIControlStateNormal];
UIImage *hoverImage = [[UIImage imageNamed:hoverImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[self setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
}
@end
#pragma mark - UIBarButtonItem
@implementation UIBarButtonItem(TcEx)
- (void)applyAppearance:(UIButtonColors)colors
{
UIView *view = [self valueForKey:@"view"];
CGFloat width = view? [view frame].size.width : (CGFloat)0.0;
[self applyAppearance:colors withSize:width];
}
- (void)applyAppearance:(UIButtonColors)colors withSize:(CGFloat)width
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, width, 30)];
[btn setTitle:self.title forState:UIControlStateNormal];
[btn applyAppearance:UIButtonColorsOrange withFont:[UIFont boldSystemFontOfSize:12]];
[self setCustomView:btn];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment