UIButton+Extras.m
4.4 KB
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
//
// UIButton+Extras.m
// DreamSleep
//
// Created by peter on 2022/4/12.
//
#import "UIButton+Extras.h"
#import <objc/runtime.h>
@implementation UIButton (Extras)
+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor imgName:(NSString *)imgName font:(UIFont *)font {
UIButton *btn = [UIButton new];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:titleColor forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
return btn;
}
+ (instancetype)dkBtnWithSubTitle:(NSString *)subTitle imgName:(NSString *)imgName font:(UIFont *)font {
UIButton *btn = [UIButton new];
[btn setTitle:subTitle forState:UIControlStateNormal];
[btn dk_setTitleColorPicker:DKColorPickerWithKey(SubTEXT) forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
return btn;
}
+ (instancetype)dkBtnTitle:(NSString *)title imgName:(NSString *)imgName font:(UIFont *)font {
UIButton *btn = [UIButton new];
[btn setTitle:title forState:UIControlStateNormal];
[btn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
return btn;
}
+ (instancetype)dkBtnTitle:(NSString *)title font:(UIFont *)font {
UIButton *btn = [UIButton new];
[btn setTitle:title forState:UIControlStateNormal];
[btn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
return btn;
}
- (void)adjustLayoutWithType:(UIButtonLayoutType)type midSpace:(CGFloat)midSpace sizeToFit:(BOOL)sizeToFit
{
if (sizeToFit) {
[self sizeToFit];
if (UIButtonLayoutTypeLeftTitleRightImage == type) {
self.width += midSpace;
}
}
CGSize titleSize;
titleSize = [[self titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
UIEdgeInsets titleEdgeInset = UIEdgeInsetsZero;
UIEdgeInsets imageEdgeInset = UIEdgeInsetsZero;
CGSize imageSize = self.imageView.frame.size;
if (UIButtonLayoutTypeLeftTitleRightImage == type) {
titleEdgeInset = UIEdgeInsetsMake(0, -imageSize.width, 0, self.imageView.width + midSpace);
imageEdgeInset = UIEdgeInsetsMake(0, titleSize.width + midSpace, 0, -titleSize.width);
} else if (UIButtonLayoutTypeUpImageBottomTitle == type) {
titleEdgeInset = UIEdgeInsetsMake(imageSize.height + midSpace, -imageSize.width, 0, 0);
imageEdgeInset = UIEdgeInsetsMake(-(titleSize.height + midSpace), 0, 0, -titleSize.width);
}
[self setTitleEdgeInsets:titleEdgeInset];
[self setImageEdgeInsets:imageEdgeInset];
}
+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font bgColor:(UIColor *)bgColor {
UIButton *btn = [UIButton new];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:titleColor forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
[btn setBackgroundColor:bgColor];
return btn;
}
+ (instancetype)btnWithTitle:(NSString *)title font:(UIFont *)font {
UIButton *btn = [UIButton new];
[btn setTitle:title forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
return btn;
}
+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font {
UIButton *btn = [UIButton new];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:titleColor forState:UIControlStateNormal];
[btn.titleLabel setFont:font];
return btn;
}
- (void)setTitle:(NSString *)title font:(UIFont *)font {
[self setTitle:title forState:UIControlStateNormal];
[self.titleLabel setFont:font];
}
static const void *kUIButtonBlockKey = &kUIButtonBlockKey;
- (void)addTouchUpInsideHandler:(kTouchUpInsideHandlerBlock)handlerBlock {
objc_setAssociatedObject(self, kUIButtonBlockKey, handlerBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(hookTouchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)hookTouchUpInsideAction:(UIButton *)sender {
kTouchUpInsideHandlerBlock block = objc_getAssociatedObject(self, kUIButtonBlockKey);
if (block) { block(sender.tag); }
}
@end