-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathALOCenteredButton.m
More file actions
executable file
·138 lines (115 loc) · 4.6 KB
/
ALOCenteredButton.m
File metadata and controls
executable file
·138 lines (115 loc) · 4.6 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// ALOCenteredButton.m
//
// Created by Alexey Yachmenev on 16.07.14.
// Copyright (c) 2014 Alexey Yachmenov. All rights reserved.
//
#import "ALOCenteredButton.h"
static CGFloat const kALODefaultImageLabelSpacing = 10.f;
@implementation ALOCenteredButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self fieldsInit];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self fieldsInit];
}
return self;
}
- (void)fieldsInit
{
_buttonOrientation = ALOCenteredButtonOrientationVertical;
_imageLabelSpacing = kALODefaultImageLabelSpacing;
}
- (void)layoutIfInSuperview
{
if (self.superview) {
[self setNeedsLayout];
[self layoutIfNeeded];
}
}
- (void)setImageLabelSpacing:(CGFloat)imageLabelSpacing
{
if (_imageLabelSpacing != imageLabelSpacing) {
_imageLabelSpacing = imageLabelSpacing;
[self layoutIfInSuperview];
}
}
- (void)setButtonOrientation:(ALOCenteredButtonOrientation)buttonOrientation
{
if (_buttonOrientation != buttonOrientation) {
_buttonOrientation = buttonOrientation;
[self layoutIfInSuperview];
}
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize labelSize = [self.titleLabel sizeThatFits:size];
CGSize imageSize = [self.imageView sizeThatFits:size];
// fix for zero width label
if (labelSize.width == 0) {
CGRect titleFrame = self.titleLabel.frame;
titleFrame.size.width = 0;
self.titleLabel.frame = titleFrame;
}
switch (self.buttonOrientation) {
case ALOCenteredButtonOrientationRightToLeft:
case ALOCenteredButtonOrientationLeftToRight:
return CGSizeMake(labelSize.width + imageSize.width + self.imageLabelSpacing + self.contentEdgeInsets.left + self.contentEdgeInsets.right,
MAX(labelSize.height, imageSize.height) + self.contentEdgeInsets.top + self.contentEdgeInsets.bottom);
case ALOCenteredButtonOrientationVertical:
return CGSizeMake(MAX(labelSize.width, imageSize.width) + self.contentEdgeInsets.left + self.contentEdgeInsets.right,
labelSize.height + imageSize.height + self.imageLabelSpacing + self.contentEdgeInsets.top + self.contentEdgeInsets.bottom);
}
}
- (CGSize)intrinsicContentSize {
return [self sizeThatFits:CGSizeZero];
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.buttonOrientation == ALOCenteredButtonOrientationVertical) {
self.titleLabel.textAlignment = NSTextAlignmentCenter;
CGFloat sumHeight = CGRectGetHeight(self.imageView.frame) + CGRectGetHeight(self.titleLabel.frame) + self.imageLabelSpacing;
// reposition image
CGRect imageFrame = self.imageView.frame;
imageFrame.origin = CGPointMake(truncf((self.bounds.size.width - self.imageView.frame.size.width) / 2),
truncf((self.bounds.size.height - sumHeight) / 2));
self.imageView.frame = imageFrame;
// reposition label
CGRect labelFrame = CGRectMake(0,
CGRectGetMaxY(imageFrame) + self.imageLabelSpacing,
self.bounds.size.width, self.titleLabel.frame.size.height);
self.titleLabel.frame = labelFrame;
return;
}
// horizontal
self.titleLabel.textAlignment = NSTextAlignmentLeft;
CGFloat sumWidth = CGRectGetWidth(self.imageView.frame) + CGRectGetWidth(self.titleLabel.frame) + self.imageLabelSpacing;
CGFloat buttonWidth = CGRectGetWidth(self.bounds);
CGRect labelFrame = self.titleLabel.frame;
if (sumWidth > buttonWidth) {
labelFrame.size.width -= sumWidth - buttonWidth;
}
CGRect imageFrame = self.imageView.frame;
CGFloat startX = truncf((buttonWidth - sumWidth) / 2);
CGFloat labelFrameY = truncf((self.bounds.size.height - labelFrame.size.height) / 2);
CGFloat imageFrameY = truncf((self.bounds.size.height - imageFrame.size.height) / 2);
if (self.buttonOrientation == ALOCenteredButtonOrientationRightToLeft) {
labelFrame.origin = CGPointMake(startX, labelFrameY);
imageFrame.origin = CGPointMake(CGRectGetMaxX(labelFrame) + self.imageLabelSpacing, imageFrameY);
} else {
imageFrame.origin = CGPointMake(startX, imageFrameY);
labelFrame.origin = CGPointMake(CGRectGetMaxX(imageFrame) + self.imageLabelSpacing, labelFrameY);
}
self.titleLabel.frame = labelFrame;
self.imageView.frame = imageFrame;
}
@end