-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUIButton+EdgeInsets.m
115 lines (92 loc) · 4.43 KB
/
UIButton+EdgeInsets.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
//
// UIButton+imageTitleLayout.m
//
//
// Created by 韩保玉 on 15/6/28.
// Copyright (c) 2015年 韩保玉. All rights reserved.
//
#import "UIButton+EdgeInsets.h"
@implementation UIButton (EdgeInsets)
- (void)layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style imageTitlespace:(CGFloat)space {
CGFloat imageViewWidth = CGRectGetWidth(self.imageView.frame);
CGFloat labelWidth = CGRectGetWidth(self.titleLabel.frame);
if (labelWidth == 0) {
CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
labelWidth = titleSize.width;
}
CGFloat imageInsetsTop = 0.0f;
CGFloat imageInsetsLeft = 0.0f;
CGFloat imageInsetsBottom = 0.0f;
CGFloat imageInsetsRight = 0.0f;
CGFloat titleInsetsTop = 0.0f;
CGFloat titleInsetsLeft = 0.0f;
CGFloat titleInsetsBottom = 0.0f;
CGFloat titleInsetsRight = 0.0f;
switch (style) {
case ButtonEdgeInsetsStyleImageRight:
{
space = space * 0.5;
imageInsetsLeft = labelWidth + space;
imageInsetsRight = -imageInsetsLeft;
titleInsetsLeft = - (imageViewWidth + space);
titleInsetsRight = -titleInsetsLeft;
}
break;
case ButtonEdgeInsetsStyleImageLeft:
{
space = space * 0.5;
imageInsetsLeft = -space;
imageInsetsRight = -imageInsetsLeft;
titleInsetsLeft = space;
titleInsetsRight = -titleInsetsLeft;
}
break;
case ButtonEdgeInsetsStyleImageBottom:
{
CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
CGFloat labelHeight = CGRectGetHeight(self.titleLabel.frame);
CGFloat buttonHeight = CGRectGetHeight(self.frame);
CGFloat boundsCentery = (imageHeight + space + labelHeight) * 0.5;
CGFloat centerX_button = CGRectGetMidX(self.bounds); // bounds
CGFloat centerX_titleLabel = CGRectGetMidX(self.titleLabel.frame);
CGFloat centerX_image = CGRectGetMidX(self.imageView.frame);
CGFloat imageBottomY = CGRectGetMaxY(self.imageView.frame);
CGFloat titleTopY = CGRectGetMinY(self.titleLabel.frame);
imageInsetsTop = buttonHeight - (buttonHeight * 0.5 - boundsCentery) - imageBottomY;
imageInsetsLeft = centerX_button - centerX_image;
imageInsetsRight = - imageInsetsLeft;
imageInsetsBottom = - imageInsetsTop;
titleInsetsTop = (buttonHeight * 0.5 - boundsCentery) - titleTopY;
titleInsetsLeft = -(centerX_titleLabel - centerX_button);
titleInsetsRight = - titleInsetsLeft;
titleInsetsBottom = - titleInsetsTop;
}
break;
case ButtonEdgeInsetsStyleImageTop:
{
CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
CGFloat labelHeight = CGRectGetHeight(self.titleLabel.frame);
CGFloat buttonHeight = CGRectGetHeight(self.frame);
CGFloat boundsCentery = (imageHeight + space + labelHeight) * 0.5;
CGFloat centerX_button = CGRectGetMidX(self.bounds); // bounds
CGFloat centerX_titleLabel = CGRectGetMidX(self.titleLabel.frame);
CGFloat centerX_image = CGRectGetMidX(self.imageView.frame);
CGFloat imageTopY = CGRectGetMinY(self.imageView.frame);
CGFloat titleBottom = CGRectGetMaxY(self.titleLabel.frame);
imageInsetsTop = (buttonHeight * 0.5 - boundsCentery) - imageTopY;
imageInsetsLeft = centerX_button - centerX_image;
imageInsetsRight = - imageInsetsLeft;
imageInsetsBottom = - imageInsetsTop;
titleInsetsTop = buttonHeight - (buttonHeight * 0.5 - boundsCentery) - titleBottom;
titleInsetsLeft = -(centerX_titleLabel - centerX_button);
titleInsetsRight = - titleInsetsLeft;
titleInsetsBottom = - titleInsetsTop;
}
break;
default:
break;
}
self.imageEdgeInsets = UIEdgeInsetsMake(imageInsetsTop, imageInsetsLeft, imageInsetsBottom, imageInsetsRight);
self.titleEdgeInsets = UIEdgeInsetsMake(titleInsetsTop, titleInsetsLeft, titleInsetsBottom, titleInsetsRight);
}
@end