iOS UI 快捷创建封装

封装了UIView、UILabel、UIButton、UIImageView的快速创建方法,让我们不用每次都进行繁杂的UI代码的编写。引用了 UIGestureRecognizer (YYAdd)。

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma mark - For UIView
+ (UIView *)createUIViewWithFrame:(CGRect)frame
bgColor:(UIColor *)bgColor
{
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = bgColor;
return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
bgColor:(UIColor *)bgColor
cornerRadius:(CGFloat)cornerRadius
{
UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor];
if (cornerRadius > 0) {
view.clipsToBounds = YES;
view.layer.cornerRadius = cornerRadius;
}
return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
bgColor:(UIColor *)bgColor
cornerRadius:(CGFloat)cornerRadius
actionGesture:(UIGestureRecognizer *)gesture
{
UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor cornerRadius:cornerRadius];
[view addGestureRecognizer:gesture];
return view;
}

+ (UIView *)createUIViewWithFrame:(CGRect)frame
bgColor:(UIColor *)bgColor
cornerRadius:(CGFloat)cornerRadius
tapAction:(void(^)())tapAction
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
if (tapAction) {
tapAction();
}
}];
UIView *view = [self createUIViewWithFrame:frame bgColor:bgColor cornerRadius:cornerRadius actionGesture:tap];
return view;
}

#pragma mark - For UILabel
+ (UILabel *)createLabelWithFrame:(CGRect)frame
text:(NSString *)text
textAlignment:(NSTextAlignment)textAlignment
fontSize:(CGFloat)fontSize
{
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
if (textAlignment) {
label.textAlignment = textAlignment;
}
if (fontSize > 0) {
label.font = [UIFont systemFontOfSize:fontSize];
}
return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
text:(NSString *)text
textAlignment:(NSTextAlignment)textAlignment
fontSize:(CGFloat)fontSize
textColor:(UIColor *)textColor
{
UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize];
label.textColor = textColor;
return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
text:(NSString *)text
textAlignment:(NSTextAlignment)textAlignment
fontSize:(CGFloat)fontSize
textColor:(UIColor *)textColor
bgColor:(UIColor *)bgColor
{
UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize textColor:textColor];
label.backgroundColor = bgColor;
return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
text:(NSString *)text
textAlignment:(NSTextAlignment)textAlignment
fontSize:(CGFloat)fontSize
textColor:(UIColor *)textColor
bgColor:(UIColor *)bgColor
cornerRadius:(CGFloat)cornerRadius
{
UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize textColor:textColor bgColor:bgColor];
if (cornerRadius > 0) {
label.clipsToBounds = YES;
label.layer.cornerRadius = cornerRadius;
}
return label;
}

+ (UILabel *)createLabelWithFrame:(CGRect)frame
text:(NSString *)text
textAlignment:(NSTextAlignment)textAlignment
fontSize:(CGFloat)fontSize
textColor:(UIColor *)textColor
bgColor:(UIColor *)bgColor
cornerRadius:(CGFloat)cornerRadius
tapAction:(void(^)())tapAction
{
UILabel *label = [self createLabelWithFrame:frame text:text textAlignment:textAlignment fontSize:fontSize textColor:textColor bgColor:bgColor cornerRadius:cornerRadius];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
if (tapAction) {
tapAction();
}
}];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tap];
return label;
}

#pragma mark - For UIButton
+ (UIButton *)createButtonWithFrame:(CGRect)frame
title:(NSString *)title
fontSize:(CGFloat)fontSize
action:(void(^)())action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
[button setTitle:title forState:UIControlStateNormal];
if (fontSize > 0) {
button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
}
[button addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
if (action) {
action();
}
}];
return button;
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame
title:(NSString *)title
fontSize:(CGFloat)fontSize
titleColor:(UIColor *)titleColor
bgColor:(UIColor *)bgColor
action:(void(^)())action
{
UIButton *button = [self createButtonWithFrame:frame title:title fontSize:fontSize action:action];
[button setTitleColor:titleColor forState:UIControlStateNormal];
button.backgroundColor = bgColor;
return button;
}

+ (UIButton *)createButtonWithFrame:(CGRect)frame
title:(NSString *)title
fontSize:(CGFloat)fontSize
titleColor:(UIColor *)titleColor
bgColor:(UIColor *)bgColor
cornerRadius:(CGFloat)cornerRadius
action:(void(^)())action
{
UIButton *button = [self createButtonWithFrame:frame title:title fontSize:fontSize titleColor:titleColor bgColor:bgColor action:action];
if (cornerRadius > 0) {
button.clipsToBounds = YES;
button.layer.cornerRadius = cornerRadius;
}
return button;
}

#pragma mark - For UIImageView
+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
imageName:(NSString *)imageName
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image = [UIImage imageNamed:imageName];
return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
cornerRadius:(CGFloat)cornerRadius
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.contentMode = UIViewContentModeScaleAspectFill;
if (cornerRadius > 0) {
imageView.clipsToBounds = YES;
imageView.layer.cornerRadius = cornerRadius;
imageView.layer.masksToBounds = YES;
}
return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
imageName:(NSString *)imageName
cornerRadius:(CGFloat)cornerRadius
{
UIImageView *imageView = [self createImageViewWithFrame:frame cornerRadius:cornerRadius];
imageView.image = [UIImage imageNamed:imageName];
return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
imageName:(NSString *)imageName
cornerRadius:(CGFloat)cornerRadius
actionGesture:(UIGestureRecognizer *)gesture
{
UIImageView *imageView = [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:gesture];
return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
imageName:(NSString *)imageName
cornerRadius:(CGFloat)cornerRadius
tapAction:(void(^)())tapAction
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
if (tapAction) {
tapAction();
}
}];
UIImageView *imageView = [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius actionGesture:tap];
return imageView;
}

+ (UIImageView *)createImageViewWithFrame:(CGRect)frame
imageName:(NSString *)imageName
roundCorner:(BOOL)roundCorner
tapAction:(void(^)())tapAction
{
CGFloat cornerRadius = 0;
if (roundCorner) {
cornerRadius = frame.size.width / 2;
}
return [self createImageViewWithFrame:frame imageName:imageName cornerRadius:cornerRadius tapAction:tapAction];
}

iOS UI 快捷创建封装
http://example.com/2016/06/30/iOS-UI-快捷创建封装/
作者
guanzhendong
发布于
2016年6月30日
许可协议