-
Notifications
You must be signed in to change notification settings - Fork 462
/
NUIButtonRenderer.m
188 lines (167 loc) · 10.7 KB
/
NUIButtonRenderer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// NUIButtonRenderer.m
// NUIDemo
//
// Created by Tom Benner on 11/24/12.
// Copyright (c) 2012 Tom Benner. All rights reserved.
//
#import "NUIButtonRenderer.h"
#import "NUIViewRenderer.h"
#import "UIButton+NUI.h"
@implementation NUIButtonRenderer
+ (void)render:(UIButton*)button withClass:(NSString*)className
{
[NUIViewRenderer renderSize:button withClass:className];
// UIButtonTypeRoundedRect's first two sublayers contain its background and border, which
// need to be hidden for NUI's rendering to be displayed correctly. Ideally we would switch
// over to a UIButtonTypeCustom, but this appears to be impossible.
if (button.buttonType == UIButtonTypeRoundedRect) {
if ([button.layer.sublayers count] > 2) {
[button.layer.sublayers[0] setOpacity:0.0f];
[button.layer.sublayers[1] setOpacity:0.0f];
}
}
// Set padding
if ([NUISettings hasProperty:@"padding" withClass:className]) {
[button setTitleEdgeInsets:[NUISettings getEdgeInsets:@"padding" withClass:className]];
}
// Set background color
if ([NUISettings hasProperty:@"background-color" withClass:className]) {
[button setBackgroundColor:[NUISettings getColor:@"background-color" withClass:className]];
}
if ([NUISettings hasProperty:@"background-color-normal" withClass:className]) {
[button setBackgroundImage:[NUISettings getImageFromColor:@"background-color-normal" withClass:className] forState:UIControlStateNormal];
}
if ([NUISettings hasProperty:@"background-color-highlighted" withClass:className]) {
[button setBackgroundImage:[NUISettings getImageFromColor:@"background-color-highlighted" withClass:className] forState:UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"background-color-selected" withClass:className]) {
[button setBackgroundImage:[NUISettings getImageFromColor:@"background-color-selected" withClass:className] forState:UIControlStateSelected];
}
if ([NUISettings hasProperty:@"background-color-selected-highlighted" withClass:className]) {
[button setBackgroundImage:[NUISettings getImageFromColor:@"background-color-selected-highlighted" withClass:className] forState:UIControlStateSelected|UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"background-color-selected-disabled" withClass:className]) {
[button setBackgroundImage:[NUISettings getImageFromColor:@"background-color-selected-disabled" withClass:className] forState:UIControlStateSelected|UIControlStateDisabled];
}
if ([NUISettings hasProperty:@"background-color-disabled" withClass:className]) {
[button setBackgroundImage:[NUISettings getImageFromColor:@"background-color-disabled" withClass:className] forState:UIControlStateDisabled];
}
// Set background gradient
if ([NUISettings hasProperty:@"background-color-top" withClass:className]) {
CAGradientLayer *gradientLayer = [NUIGraphics
gradientLayerWithTop:[NUISettings getColor:@"background-color-top" withClass:className]
bottom:[NUISettings getColor:@"background-color-bottom" withClass:className]
frame:button.bounds];
if (button.gradientLayer) {
[button.layer replaceSublayer:button.gradientLayer with:gradientLayer];
} else {
int backgroundLayerIndex = [button.layer.sublayers count] == 1 ? 0 : 1;
[button.layer insertSublayer:gradientLayer atIndex:backgroundLayerIndex];
}
button.gradientLayer = gradientLayer;
}
// Set background image
if ([NUISettings hasProperty:@"background-image" withClass:className]) {
[button setBackgroundImage:[NUISettings getImage:@"background-image" withClass:className] forState:UIControlStateNormal];
}
if ([NUISettings hasProperty:@"background-image-highlighted" withClass:className]) {
[button setBackgroundImage:[NUISettings getImage:@"background-image-highlighted" withClass:className] forState:UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"background-image-selected" withClass:className]) {
[button setBackgroundImage:[NUISettings getImage:@"background-image-selected" withClass:className] forState:UIControlStateSelected];
}
if ([NUISettings hasProperty:@"background-image-selected-highlighted" withClass:className]) {
[button setBackgroundImage:[NUISettings getImage:@"background-image-selected-highlighted" withClass:className] forState:UIControlStateSelected|UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"background-image-selected-disabled" withClass:className]) {
[button setBackgroundImage:[NUISettings getImage:@"background-image-selected-disabled" withClass:className] forState:UIControlStateSelected|UIControlStateDisabled];
}
if ([NUISettings hasProperty:@"background-image-disabled" withClass:className]) {
[button setBackgroundImage:[NUISettings getImage:@"background-image-disabled" withClass:className] forState:UIControlStateDisabled];
}
// Set image
if ([NUISettings hasProperty:@"image" withClass:className]) {
[button setImage:[NUISettings getImage:@"image" withClass:className] forState:UIControlStateNormal];
}
if ([NUISettings hasProperty:@"image-highlighted" withClass:className]) {
[button setImage:[NUISettings getImage:@"image-highlighted" withClass:className] forState:UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"image-selected" withClass:className]) {
[button setImage:[NUISettings getImage:@"image-selected" withClass:className] forState:UIControlStateSelected];
}
if ([NUISettings hasProperty:@"image-selected-highlighted" withClass:className]) {
[button setImage:[NUISettings getImage:@"image-selected-highlighted" withClass:className] forState:UIControlStateSelected|UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"image-selected-disabled" withClass:className]) {
[button setImage:[NUISettings getImage:@"image-selected-disabled" withClass:className] forState:UIControlStateSelected|UIControlStateDisabled];
}
if ([NUISettings hasProperty:@"image-disabled" withClass:className]) {
[button setImage:[NUISettings getImage:@"image-disabled" withClass:className] forState:UIControlStateDisabled];
}
[NUILabelRenderer renderText:button.titleLabel withClass:className];
// Set text align
if ([NUISettings hasProperty:@"text-align" withClass:className]) {
[button setContentHorizontalAlignment:[NUISettings getControlContentHorizontalAlignment:@"text-align" withClass:className]];
}
// Set font color
if ([NUISettings hasProperty:@"font-color" withClass:className]) {
[button setTitleColor:[NUISettings getColor:@"font-color" withClass:className] forState:UIControlStateNormal];
}
if ([NUISettings hasProperty:@"font-color-highlighted" withClass:className]) {
[button setTitleColor:[NUISettings getColor:@"font-color-highlighted" withClass:className] forState:UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"font-color-selected" withClass:className]) {
[button setTitleColor:[NUISettings getColor:@"font-color-selected" withClass:className] forState:UIControlStateSelected];
}
if ([NUISettings hasProperty:@"font-color-selected-highlighted" withClass:className]) {
[button setTitleColor:[NUISettings getColor:@"font-color-selected-highlighted" withClass:className] forState:UIControlStateSelected|UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"font-color-selected-disabled" withClass:className]) {
[button setTitleColor:[NUISettings getColor:@"font-color-selected-disabled" withClass:className] forState:UIControlStateSelected|UIControlStateDisabled];
}
if ([NUISettings hasProperty:@"font-color-disabled" withClass:className]) {
[button setTitleColor:[NUISettings getColor:@"font-color-disabled" withClass:className] forState:UIControlStateDisabled];
}
// Set text shadow color
if ([NUISettings hasProperty:@"text-shadow-color" withClass:className]) {
[button setTitleShadowColor:[NUISettings getColor:@"text-shadow-color" withClass:className] forState:UIControlStateNormal];
}
if ([NUISettings hasProperty:@"text-shadow-color-highlighted" withClass:className]) {
[button setTitleShadowColor:[NUISettings getColor:@"text-shadow-color-highlighted" withClass:className] forState:UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"text-shadow-color-selected" withClass:className]) {
[button setTitleShadowColor:[NUISettings getColor:@"text-shadow-color-selected" withClass:className] forState:UIControlStateSelected];
}
if ([NUISettings hasProperty:@"text-shadow-color-selected-highlighted" withClass:className]) {
[button setTitleShadowColor:[NUISettings getColor:@"text-shadow-color-selected-highlighted" withClass:className] forState:UIControlStateSelected|UIControlStateHighlighted];
}
if ([NUISettings hasProperty:@"text-shadow-color-selected-disabled" withClass:className]) {
[button setTitleShadowColor:[NUISettings getColor:@"text-shadow-color-selected-disabled" withClass:className] forState:UIControlStateSelected|UIControlStateDisabled];
}
if ([NUISettings hasProperty:@"text-shadow-color-disabled" withClass:className]) {
[button setTitleShadowColor:[NUISettings getColor:@"text-shadow-color-disabled" withClass:className] forState:UIControlStateDisabled];
}
// title insets
if ([NUISettings hasProperty:@"title-insets" withClass:className]) {
[button setTitleEdgeInsets:[NUISettings getEdgeInsets:@"title-insets" withClass:className]];
}
// content insets
if ([NUISettings hasProperty:@"content-insets" withClass:className]) {
[button setContentEdgeInsets:[NUISettings getEdgeInsets:@"content-insets" withClass:className]];
}
[NUIViewRenderer renderBorder:button withClass:className];
// If a shadow-* is configured and corner-radius is set disable mask to bounds and fall back to manually applying corner radius to all sub-views (except the label)
if ([NUIViewRenderer hasShadowProperties:button withClass:className] &&
[NUISettings hasProperty:@"corner-radius" withClass:className]) {
CGFloat r = [NUISettings getFloat:@"corner-radius" withClass:className];
for (UIView* subview in button.subviews) {
if ([subview isKindOfClass:[UILabel class]] == NO) {
subview.layer.cornerRadius = r;
}
}
button.layer.masksToBounds = NO;
}
[NUIViewRenderer renderShadow:button withClass:className];
}
@end