-
Notifications
You must be signed in to change notification settings - Fork 1
/
StretchButton.m
198 lines (170 loc) · 4.19 KB
/
StretchButton.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
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
//
// StretchButton.m
//
// Copyright (c) 2012 Symbiotic Software LLC. All rights reserved.
//
#import "StretchButton.h"
@interface StretchButton ()
{
NSInteger leftCap;
NSInteger topCap;
}
- (void)setup;
- (void)resize;
@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) UIImageView *iconImageView;
@end
@implementation StretchButton
@synthesize text;
@synthesize textColor;
@synthesize font;
@synthesize textLabel;
@synthesize iconImageView;
@synthesize icon;
@synthesize fitToIcon;
@synthesize disableLeftMargin;
- (void)setup
{
self.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.clipsToBounds = NO;
self.opaque = NO;
self.textLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.backgroundColor = [UIColor clearColor];
self.textLabel.textAlignment = UITextAlignmentCenter;
[self addSubview:self.textLabel];
}
- (void)resize
{
CGRect frame;
CGSize size, imageSize;
if(self.iconImageView)
{
frame = self.frame;
size = [self.textLabel.text sizeWithFont:self.textLabel.font];
imageSize = self.iconImageView.frame.size;
frame.size.width = (leftCap * 2.0) + size.width + imageSize.width;
if(self.fitToIcon)
frame.size.height = (topCap * 2.0) + imageSize.height;
else
frame.size.height = (topCap * 2.0) + ((imageSize.height > size.height)?imageSize.height:size.height);
}
else
{
// Resize the button to fit the text
frame = self.frame;
size = [self.textLabel.text sizeWithFont:self.textLabel.font];
frame.size.width = (leftCap * 2.0) + size.width;
frame.size.height = (topCap * 2.0) + size.height;
}
self.frame = frame;
[self setNeedsLayout];
}
+ (StretchButton *)stretchedButtonWithText:(NSString *)theText textColor:(UIColor *)theTextColor font:(UIFont *)theFont image:(UIImage *)stretchableImage leftCap:(NSInteger)theLeftCap topCap:(NSInteger)theTopCap
{
UIImage *image;
StretchButton *button;
button = [[[StretchButton alloc] initWithFrame:CGRectZero] autorelease];
button->leftCap = theLeftCap;
button->topCap = theTopCap;
image = [stretchableImage stretchableImageWithLeftCapWidth:theLeftCap topCapHeight:theTopCap];
[button setImage:image forState:UIControlStateNormal];
button.text = theText;
button.textColor = theTextColor;
button.font = theFont;
return button;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self setup];
[self resize];
}
return self;
}
- (void)dealloc
{
self.textLabel = nil;
self.iconImageView = nil;
[super dealloc];
}
- (NSString *)text
{
return self.textLabel.text;
}
- (void)setText:(NSString *)newText
{
self.textLabel.text = newText;
[self resize];
}
- (UIColor *)textColor
{
return textLabel.textColor;
}
- (void)setTextColor:(UIColor *)newTextColor
{
self.textLabel.textColor = newTextColor;
}
- (UIFont *)font
{
return self.textLabel.font;
}
- (void)setFont:(UIFont *)newFont
{
self.textLabel.font = newFont;
[self resize];
}
- (UIImage *)icon
{
if(self.iconImageView)
return self.iconImageView.image;
return nil;
}
- (void)setIcon:(UIImage *)newIcon
{
if(newIcon)
{
if(!self.iconImageView)
{
self.iconImageView = [[[UIImageView alloc] initWithImage:newIcon] autorelease];
[self addSubview:self.iconImageView];
}
self.iconImageView.image = newIcon;
self.textLabel.textAlignment = UITextAlignmentRight;
}
else
{
[self.iconImageView removeFromSuperview];
self.iconImageView = nil;
self.textLabel.textAlignment = UITextAlignmentCenter;
}
[self resize];
}
- (void)setFitToIcon:(BOOL)newFitToIcon
{
fitToIcon = newFitToIcon;
[self resize];
}
- (void)layoutSubviews
{
CGRect frame;
[super layoutSubviews];
frame = self.bounds;
frame.origin.x = leftCap;
frame.origin.y = topCap;
frame.size.width -= (leftCap * 2.0f);
frame.size.height -= (topCap * 2.0f);
self.textLabel.frame = frame;
if(self.iconImageView)
{
frame.size = self.iconImageView.frame.size;
frame.origin.y = topCap + ((self.bounds.size.height - topCap * 2.0f) - frame.size.height)/2.0f;
if(self.disableLeftMargin)
frame.origin.x = 0.0f;
self.iconImageView.frame = frame;
}
}
@end