-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUIButton.m
executable file
·87 lines (72 loc) · 2.84 KB
/
FUIButton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// FUIButton.m
// FlatUI
//
// Created by Jack Flintermann on 5/7/13.
// Copyright (c) 2013 Jack Flintermann. All rights reserved.
//
#import "FUIButton.h"
#import "UIImage+FlatUI.h"
@interface FUIButton()
@property(nonatomic) UIEdgeInsets defaultEdgeInsets;
@property(nonatomic) UIEdgeInsets normalEdgeInsets;
@property(nonatomic) UIEdgeInsets highlightedEdgeInsets;
@end
@implementation FUIButton
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.defaultEdgeInsets = self.titleEdgeInsets;
}
return self;
}
- (void) setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
[super setTitleEdgeInsets:titleEdgeInsets];
self.defaultEdgeInsets = titleEdgeInsets;
[self setShadowHeight:self.shadowHeight];
}
- (void) setHighlighted:(BOOL)highlighted {
UIEdgeInsets insets = highlighted ? self.highlightedEdgeInsets : self.normalEdgeInsets;
[super setTitleEdgeInsets:insets];
[super setHighlighted:highlighted];
}
- (void) setCornerRadius:(CGFloat)cornerRadius {
_cornerRadius = cornerRadius;
[self configureFlatButton];
}
- (void) setButtonColor:(UIColor *)buttonColor {
_buttonColor = buttonColor;
[self configureFlatButton];
}
- (void) setShadowColor:(UIColor *)shadowColor {
_shadowColor = shadowColor;
[self configureFlatButton];
}
- (void) setHighlightedColor:(UIColor *)highlightedColor{
_highlightedColor = highlightedColor;
[self configureFlatButton];
}
- (void) setShadowHeight:(CGFloat)shadowHeight {
_shadowHeight = shadowHeight;
UIEdgeInsets insets = self.defaultEdgeInsets;
insets.top += shadowHeight;
self.highlightedEdgeInsets = insets;
insets.top -= shadowHeight * 2.0f;
self.normalEdgeInsets = insets;
[super setTitleEdgeInsets:insets];
[self configureFlatButton];
}
- (void) configureFlatButton {
UIImage *normalBackgroundImage = [UIImage buttonImageWithColor:self.buttonColor
cornerRadius:self.cornerRadius
shadowColor:self.shadowColor
shadowInsets:UIEdgeInsetsMake(0, 0, self.shadowHeight, 0)];
UIColor *color = self.highlightedColor == nil ? self.buttonColor : self.highlightedColor;
UIImage *highlightedBackgroundImage = [UIImage buttonImageWithColor:color
cornerRadius:self.cornerRadius
shadowColor:[UIColor clearColor]
shadowInsets:UIEdgeInsetsMake(self.shadowHeight, 0, 0, 0)];
[self setBackgroundImage:normalBackgroundImage forState:UIControlStateNormal];
[self setBackgroundImage:highlightedBackgroundImage forState:UIControlStateHighlighted];
}
@end