/
TiUIButton.m
368 lines (326 loc) Β· 9.73 KB
/
TiUIButton.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_UIBUTTON
#import "TiUIButton.h"
#import "TiUIButtonProxy.h"
#import "TiUtils.h"
#import "ImageLoader.h"
#import "TiButtonUtil.h"
#import "TiUIView.h"
@implementation TiUIButton
#pragma mark Internal
-(void)dealloc
{
[button removeTarget:self action:NULL forControlEvents:UIControlEventAllTouchEvents];
RELEASE_TO_NIL(button);
RELEASE_TO_NIL(viewGroupWrapper);
RELEASE_TO_NIL(backgroundImageCache)
RELEASE_TO_NIL(backgroundImageUnstretchedCache);
[super dealloc];
}
-(UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *superResult = [super hitTest:point withEvent:event];
if(superResult == nil) {
return nil;
}
if((viewGroupWrapper == superResult) || ([superResult isKindOfClass:[TiUIView class]]
&& ![(TiUIView*)superResult touchEnabled])) {
return [self button];
}
return superResult;
}
-(BOOL)hasTouchableListener
{
// since this guy only works with touch events, we always want them
// just always return YES no matter what listeners we have registered
return YES;
}
-(void)setHighlighting:(BOOL)isHiglighted
{
for (TiUIView * thisView in [viewGroupWrapper subviews])
{
if ([thisView respondsToSelector:@selector(setHighlighted:)])
{
[(id)thisView setHighlighted:isHiglighted];
}
}
}
-(void)updateBackgroundImage
{
CGRect bounds = [self bounds];
[button setFrame:bounds];
if ((backgroundImageCache == nil) || (bounds.size.width == 0) || (bounds.size.height == 0)) {
[button setBackgroundImage:nil forState:UIControlStateNormal];
return;
}
CGSize imageSize = [backgroundImageCache size];
if((bounds.size.width>=imageSize.width) && (bounds.size.height>=imageSize.height)){
[button setBackgroundImage:backgroundImageCache forState:UIControlStateNormal];
return;
}
//If the bounds are smaller than the image size render it in an imageView and get the image of the view.
//Should be pretty inexpensive since it happens rarely. TIMOB-9166
CGSize unstrechedSize = (backgroundImageUnstretchedCache != nil) ? [backgroundImageUnstretchedCache size] : CGSizeZero;
if (backgroundImageUnstretchedCache == nil || !CGSizeEqualToSize(unstrechedSize,bounds.size) ) {
UIImageView* theView = [[UIImageView alloc] initWithFrame:bounds];
[theView setImage:backgroundImageCache];
UIGraphicsBeginImageContextWithOptions(bounds.size, [theView.layer isOpaque], 0.0);
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
RELEASE_TO_NIL(backgroundImageUnstretchedCache);
backgroundImageUnstretchedCache = [image retain];
[theView release];
}
[button setBackgroundImage:backgroundImageUnstretchedCache forState:UIControlStateNormal];
}
-(void)frameSizeChanged:(CGRect)frame bounds:(CGRect)bounds
{
[super frameSizeChanged:frame bounds:bounds];
[self updateBackgroundImage];
}
- (void)controlAction:(id)sender forEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
NSString *fireEvent;
NSString * fireActionEvent = nil;
switch (touch.phase) {
case UITouchPhaseBegan:
if (touchStarted) {
return;
}
touchStarted = YES;
fireEvent = @"touchstart";
break;
case UITouchPhaseMoved:
fireEvent = @"touchmove";
break;
case UITouchPhaseEnded:
touchStarted = NO;
fireEvent = @"touchend";
if (button.highlighted) {
fireActionEvent = [touch tapCount] == 1 ? @"click" : ([touch tapCount] == 2 ? @"dblclick" : nil);
}
break;
case UITouchPhaseCancelled:
touchStarted = NO;
fireEvent = @"touchcancel";
break;
default:
return;
}
[self setHighlighting:button.highlighted];
NSMutableDictionary *evt = [NSMutableDictionary dictionaryWithDictionary:[TiUtils pointToDictionary:[touch locationInView:self]]];
if ((fireActionEvent != nil) && [self.proxy _hasListeners:fireActionEvent]) {
[self.proxy fireEvent:fireActionEvent withObject:evt];
}
if ([self.proxy _hasListeners:fireEvent]) {
[self.proxy fireEvent:fireEvent withObject:evt];
}
}
-(UIButton*)button
{
if (button==nil)
{
BOOL hasImage = [self.proxy valueForKey:@"backgroundImage"]!=nil;
UIButtonType defaultType = (hasImage==YES) ? UIButtonTypeCustom : UIButtonTypeRoundedRect;
style = [TiUtils intValue:[self.proxy valueForKey:@"style"] def:defaultType];
UIView *btn = [TiButtonUtil buttonWithType:style];
button = (UIButton*)[btn retain];
[self addSubview:button];
if (style==UIButtonTypeCustom)
{
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
[button addTarget:self action:@selector(controlAction:forEvent:) forControlEvents:UIControlEventAllTouchEvents];
button.exclusiveTouch = YES;
}
if ((viewGroupWrapper != nil) && ([viewGroupWrapper superview]!=button)) {
[viewGroupWrapper setFrame:[button bounds]];
[button addSubview:viewGroupWrapper];
}
return button;
}
- (id)accessibilityElement
{
return [self button];
}
-(UIView *) viewGroupWrapper
{
if (viewGroupWrapper == nil) {
viewGroupWrapper = [[UIView alloc] initWithFrame:[self bounds]];
[viewGroupWrapper setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
}
if (button != [viewGroupWrapper superview]) {
if (button != nil) {
[viewGroupWrapper setFrame:[button bounds]];
[button addSubview:viewGroupWrapper];
}
else {
[viewGroupWrapper removeFromSuperview];
}
}
return viewGroupWrapper;
}
#pragma mark Public APIs
-(void)setStyle_:(id)style_
{
int s = [TiUtils intValue:style_ def:UIButtonTypeCustom];
if (s == style)
{
return;
}
style = s;
if (button==nil)
{
return;
}
RELEASE_TO_NIL(button);
[self button];
}
-(void)setImage_:(id)value
{
UIImage *image = value==nil ? nil : [TiUtils image:value proxy:(TiProxy*)self.proxy];
if (image!=nil)
{
[[self button] setImage:image forState:UIControlStateNormal];
[(TiViewProxy *)[self proxy] contentsWillChange];
}
else
{
[[self button] setImage:nil forState:UIControlStateNormal];
}
}
-(void)setEnabled_:(id)value
{
[[self button] setEnabled:[TiUtils boolValue:value]];
}
-(void)setTitle_:(id)value
{
[[self button] setTitle:[TiUtils stringValue:value] forState:UIControlStateNormal];
}
-(void)setBackgroundImage_:(id)value
{
[backgroundImageCache release];
RELEASE_TO_NIL(backgroundImageUnstretchedCache);
backgroundImageCache = [[self loadImage:value] retain];
self.backgroundImage = value;
[self updateBackgroundImage];
}
-(void)setBackgroundSelectedImage_:(id)value
{
[[self button] setBackgroundImage:[self loadImage:value] forState:UIControlStateHighlighted];
}
-(void)setBackgroundDisabledImage_:(id)value
{
[[self button] setBackgroundImage:[self loadImage:value] forState:UIControlStateDisabled];
}
-(void)setBackgroundFocusedImage_:(id)value
{
[[self button] setBackgroundImage:[self loadImage:value] forState:UIControlStateSelected];
}
-(void)setBackgroundColor_:(id)value
{
if (value!=nil)
{
TiColor *color = [TiUtils colorValue:value];
[[self button] setBackgroundColor:[color _color]];
}
}
-(void)setFont_:(id)font
{
if (font!=nil)
{
WebFont *f = [TiUtils fontValue:font def:nil];
[[[self button] titleLabel] setFont:[f font]];
}
}
-(void)setColor_:(id)color
{
if (color!=nil)
{
TiColor *c = [TiUtils colorValue:color];
UIButton *b = [self button];
if (c!=nil)
{
[b setTitleColor:[c _color] forState:UIControlStateNormal];
}
else if (b.buttonType==UIButtonTypeCustom)
{
[b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
}
-(void)setSelectedColor_:(id)color
{
if (color!=nil)
{
TiColor *selColor = [TiUtils colorValue:color];
UIButton *b = [self button];
if (selColor!=nil)
{
[b setTitleColor:[selColor _color] forState:UIControlStateHighlighted];
}
else if (b.buttonType==UIButtonTypeCustom)
{
[b setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
}
}
-(void)setDisabledColor_:(id)color
{
if (color!=nil) {
TiColor *selColor = [TiUtils colorValue:color];
UIButton *b = [self button];
if (selColor!=nil) {
[b setTitleColor:[selColor _color] forState:UIControlStateDisabled];
}
}
}
-(void)setShadowColor_:(id)color
{
if (color==nil) {
[[self button] setTitleShadowColor:nil forState:UIControlStateNormal];
} else {
color = [TiUtils colorValue:color];
[[self button] setTitleShadowColor:[color color] forState:UIControlStateNormal];
}
}
-(void)setShadowOffset_:(id)value
{
CGPoint p = [TiUtils pointValue:value];
CGSize size = {p.x,p.y};
[[[self button] titleLabel] setShadowOffset:size];
}
-(void)setTextAlign_:(id)align
{
UIButton *b = [self button];
if ([align isEqual:@"left"])
{
b.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
b.contentEdgeInsets = UIEdgeInsetsMake(0,10,0,0);
}
else if ([align isEqual:@"right"])
{
b.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
b.contentEdgeInsets = UIEdgeInsetsMake(0,0,10,0);
}
else if ([align isEqual:@"center"])
{
b.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
}
-(CGFloat)contentWidthForWidth:(CGFloat)value
{
return [[self button] sizeThatFits:CGSizeMake(value, 0)].width;
}
-(CGFloat)contentHeightForWidth:(CGFloat)value
{
return [[self button] sizeThatFits:CGSizeMake(value, 0)].height;
}
@end
#endif