/
UIButton+Custom.m
158 lines (138 loc) · 5.29 KB
/
UIButton+Custom.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
//
// UIButton+Custom.m
// PayBao
//
// Created by 謝傳友 on 15/7/18.
// Copyright (c) 2015年 PAYBAO. All rights reserved.
//
#import "UIButton+Custom.h"
#import "Constant.h"
@implementation UIButton (Custom)
+ (UIButton *)buttonWithType:(UIButtonType)type
frame:(CGRect)frame
title:(NSString *)title
titleColor:(UIColor *)titleColor
titleFontSize:(float)titleFontSize
backgroundColor:(UIColor *)backgroundColor
normarBackgroundImage:(UIImage *)normarBackgroundImage
highLightBackgroundImage:(UIImage *)highLightBackgroundImage
{
UIButton *button = [UIButton buttonWithType:type];
button.frame = frame;
[button setTitle:title forState:UIControlStateNormal];
if (titleColor) {
[button setTitleColor:titleColor forState:UIControlStateNormal];
}
button.titleLabel.font = [UIFont systemFontOfSize:titleFontSize > 0 ? titleFontSize : 0];
if (backgroundColor) {
[button setBackgroundColor:backgroundColor];
}
if (normarBackgroundImage) {
[button setBackgroundImage:normarBackgroundImage forState:UIControlStateNormal];
}
if (highLightBackgroundImage) {
[button setBackgroundImage:highLightBackgroundImage forState:UIControlStateHighlighted];
}
return button;
}
+ (UIButton *)buttonWithType:(UIButtonType)type
frame:(CGRect)frame
title:(NSString *)title
titleColor:(UIColor *)titleColor
titleFontSize:(float)titleFontSize
backgroundColor:(UIColor *)backgroundColor
normarImage:(UIImage *)normarImage
highLightImage:(UIImage *)highLightImage
{
UIButton *button = [UIButton buttonWithType:type];
button.frame = frame;
[button setTitle:title forState:UIControlStateNormal];
if (titleColor) {
[button setTitleColor:titleColor forState:UIControlStateNormal];
}
button.titleLabel.font = [UIFont systemFontOfSize:titleFontSize > 0 ? titleFontSize : 0];
if (backgroundColor) {
[button setBackgroundColor:backgroundColor];
}
if (normarImage) {
[button setImage:normarImage forState:UIControlStateNormal];
}
if (highLightImage) {
[button setImage:highLightImage forState:UIControlStateHighlighted];
}
return button;
}
+ (UIButton *)createPlusSignButton;
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 48, 48);
button.layer.cornerRadius = 22.5;
[button setBackgroundImage:[UIImage imageNamed:@"plus_sign"] forState:UIControlStateNormal];
return button;
}
+ (UIButton *)createCloseSignButton
{
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame = CGRectMake(0, 0, 48, 48);
closeButton.layer.cornerRadius = 22.5;
[closeButton setBackgroundImage:[UIImage imageNamed:@"close_sign"] forState:UIControlStateNormal];
return closeButton;
}
- (void)loadImageWithURLString:(NSString *)urlString
{
if (!urlString || [urlString isEqualToString:@""]) {
return;
}
NSURL *url = [NSURL URLWithString:urlString];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
dispatch_sync(dispatch_get_main_queue(), ^{
if (image) {
[self setImage:image forState:UIControlStateNormal];
}
});
});
}
- (void)loadBackgroundImageWithURLString:(NSString *)urlString
{
if (!urlString || [urlString isEqualToString:@""]) {
return;
}
NSURL *url = [NSURL URLWithString:urlString];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
dispatch_sync(dispatch_get_main_queue(), ^{
if (image) {
[self setBackgroundImage:image forState:UIControlStateNormal];
}
});
});
}
- (void)loadBackgroundImageWithURLString:(NSString *)urlString complete:(void (^)(UIImage *image))complete
{
if (!urlString || [urlString isEqualToString:@""]) {
return;
}
NSURL *url = [NSURL URLWithString:urlString];
__weak UIButton *weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
__strong UIButton *strongSelf = weakSelf;
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
dispatch_sync(dispatch_get_main_queue(), ^{
if (image) {
[strongSelf setBackgroundImage:image forState:UIControlStateNormal];
}
complete(image);
});
});
}
- (void)setupContentVerticalAlignmentWithYSpace:(CGFloat)ySpace
{
CGFloat imageWidth = self.imageView.bounds.size.width;
CGFloat imageHeight = self.imageView.bounds.size.height;
CGFloat titleWidth = self.titleLabel.bounds.size.width;
CGFloat titleHeight = self.titleLabel.bounds.size.height;
self.imageEdgeInsets = UIEdgeInsetsMake(-0.5 * (titleHeight + ySpace), 0.5 * titleWidth, 0.5 * (titleHeight + ySpace), -0.5 * titleWidth);
self.titleEdgeInsets = UIEdgeInsetsMake(0.5 * (imageHeight + ySpace), -0.5*imageWidth, -0.5 * (imageHeight + ySpace), 0.5*imageWidth);
}
@end