/
BlockAlertView.m
executable file
·350 lines (301 loc) · 13.1 KB
/
BlockAlertView.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
//
// BlockAlertView.m
//
//
#import "BlockAlertView.h"
#import "BlockBackground.h"
#import "BlockUI.h"
@implementation BlockAlertView
@synthesize view = _view;
@synthesize backgroundImage = _backgroundImage;
@synthesize vignetteBackground = _vignetteBackground;
static UIImage *background = nil;
static UIFont *titleFont = nil;
static UIFont *messageFont = nil;
static UIFont *buttonFont = nil;
#pragma mark - init
+ (void)initialize
{
if (self == [BlockAlertView class])
{
background = [UIImage imageNamed:kAlertViewBackground];
background = [[background stretchableImageWithLeftCapWidth:0 topCapHeight:kAlertViewBackgroundCapHeight] retain];
titleFont = [kAlertViewTitleFont retain];
messageFont = [kAlertViewMessageFont retain];
buttonFont = [kAlertViewButtonFont retain];
}
}
+ (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message
{
return [[[BlockAlertView alloc] initWithTitle:title message:message] autorelease];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSObject
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
if ((self = [super init]))
{
UIWindow *parentView = [BlockBackground sharedInstance];
CGRect frame = parentView.bounds;
frame.origin.x = floorf((frame.size.width - background.size.width) * 0.5);
frame.size.width = background.size.width;
_view = [[UIView alloc] initWithFrame:frame];
_blocks = [[NSMutableArray alloc] init];
_height = kAlertViewBorder + 6;
if (title)
{
CGSize size = [title sizeWithFont:titleFont
constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000)
lineBreakMode:UILineBreakModeWordWrap];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)];
labelView.font = titleFont;
labelView.numberOfLines = 0;
labelView.lineBreakMode = UILineBreakModeWordWrap;
labelView.textColor = kAlertViewTitleTextColor;
labelView.backgroundColor = [UIColor clearColor];
labelView.textAlignment = UITextAlignmentCenter;
labelView.shadowColor = kAlertViewTitleShadowColor;
labelView.shadowOffset = kAlertViewTitleShadowOffset;
labelView.text = title;
[_view addSubview:labelView];
[labelView release];
_height += size.height + kAlertViewBorder;
}
if (message)
{
CGSize size = [message sizeWithFont:messageFont
constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000)
lineBreakMode:UILineBreakModeWordWrap];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)];
labelView.font = messageFont;
labelView.numberOfLines = 0;
labelView.lineBreakMode = UILineBreakModeWordWrap;
labelView.textColor = kAlertViewMessageTextColor;
labelView.backgroundColor = [UIColor clearColor];
labelView.textAlignment = UITextAlignmentCenter;
labelView.shadowColor = kAlertViewMessageShadowColor;
labelView.shadowOffset = kAlertViewMessageShadowOffset;
labelView.text = message;
[_view addSubview:labelView];
[labelView release];
_height += size.height + kAlertViewBorder;
}
_vignetteBackground = YES;
}
return self;
}
- (void)dealloc
{
[_backgroundImage release];
[_view release];
[_blocks release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Public
- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block
{
[_blocks addObject:[NSArray arrayWithObjects:
block ? [[block copy] autorelease] : [NSNull null],
title,
color,
nil]];
}
- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:@"gray" block:block];
}
- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:@"black" block:block];
}
- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:@"red" block:block];
}
- (void)show
{
BOOL isSecondButton = NO;
NSUInteger index = 0;
for (NSUInteger i = 0; i < _blocks.count; i++)
{
NSArray *block = [_blocks objectAtIndex:i];
NSString *title = [block objectAtIndex:1];
NSString *color = [block objectAtIndex:2];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", color]];
image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0];
CGFloat maxHalfWidth = floorf((_view.bounds.size.width-kAlertViewBorder*3)*0.5);
CGFloat width = _view.bounds.size.width-kAlertViewBorder*2;
CGFloat xOffset = kAlertViewBorder;
if (isSecondButton)
{
width = maxHalfWidth;
xOffset = width + kAlertViewBorder * 2;
isSecondButton = NO;
}
else if (i + 1 < _blocks.count)
{
// In this case there's another button.
// Let's check if they fit on the same line.
CGSize size = [title sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width-kAlertViewBorder*2
lineBreakMode:UILineBreakModeClip];
if (size.width < maxHalfWidth - kAlertViewBorder)
{
// It might fit. Check the next Button
NSArray *block2 = [_blocks objectAtIndex:i+1];
NSString *title2 = [block2 objectAtIndex:1];
size = [title2 sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width-kAlertViewBorder*2
lineBreakMode:UILineBreakModeClip];
if (size.width < maxHalfWidth - kAlertViewBorder)
{
// They'll fit!
isSecondButton = YES; // For the next iteration
width = maxHalfWidth;
}
}
}
else if (_blocks.count == 1)
{
// In this case this is the ony button. We'll size according to the text
CGSize size = [title sizeWithFont:buttonFont
minFontSize:10
actualFontSize:nil
forWidth:_view.bounds.size.width-kAlertViewBorder*2
lineBreakMode:UILineBreakModeClip];
size.width = MAX(size.width, 80);
if (size.width + 2 * kAlertViewBorder < width)
{
width = size.width + 2 * kAlertViewBorder;
xOffset = floorf((_view.bounds.size.width - width) * 0.5);
}
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(xOffset, _height, width, kAlertButtonHeight);
button.titleLabel.font = buttonFont;
button.titleLabel.minimumFontSize = 10;
button.titleLabel.textAlignment = UITextAlignmentCenter;
button.titleLabel.shadowOffset = kAlertViewButtonShadowOffset;
button.backgroundColor = [UIColor clearColor];
button.tag = i+1;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitleColor:kAlertViewButtonTextColor forState:UIControlStateNormal];
[button setTitleShadowColor:kAlertViewButtonShadowColor forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
button.accessibilityLabel = title;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_view addSubview:button];
if (!isSecondButton)
_height += kAlertButtonHeight + kAlertViewBorder;
index++;
}
_height += 10; // Margin for the shadow
if (_height < background.size.height)
{
CGFloat offset = background.size.height - _height;
_height = background.size.height;
CGRect frame;
for (NSUInteger i = 0; i < _blocks.count; i++)
{
UIButton *btn = (UIButton *)[_view viewWithTag:i+1];
frame = btn.frame;
frame.origin.y += offset;
btn.frame = frame;
}
}
CGRect frame = _view.frame;
frame.origin.y = - _height;
frame.size.height = _height;
_view.frame = frame;
UIImageView *modalBackground = [[UIImageView alloc] initWithFrame:_view.bounds];
modalBackground.image = background;
modalBackground.contentMode = UIViewContentModeScaleToFill;
[_view insertSubview:modalBackground atIndex:0];
[modalBackground release];
if (_backgroundImage)
{
[BlockBackground sharedInstance].backgroundImage = _backgroundImage;
[_backgroundImage release];
_backgroundImage = nil;
}
[BlockBackground sharedInstance].vignetteBackground = _vignetteBackground;
[[BlockBackground sharedInstance] addToMainWindow:_view];
__block CGPoint center = _view.center;
center.y = floorf([BlockBackground sharedInstance].bounds.size.height * 0.5) + kAlertViewBounce;
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:^{
[BlockBackground sharedInstance].alpha = 1.0f;
_view.center = center;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.1
delay:0.0
options:0
animations:^{
center.y -= kAlertViewBounce;
_view.center = center;
}
completion:nil];
}];
[self retain];
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
if (buttonIndex >= 0 && buttonIndex < [_blocks count])
{
id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0];
if (![obj isEqual:[NSNull null]])
{
((void (^)())obj)();
}
}
if (animated)
{
[UIView animateWithDuration:0.1
delay:0.0
options:0
animations:^{
CGPoint center = _view.center;
center.y += 20;
_view.center = center;
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationCurveEaseIn
animations:^{
CGRect frame = _view.frame;
frame.origin.y = -frame.size.height;
_view.frame = frame;
[[BlockBackground sharedInstance] reduceAlphaIfEmpty];
}
completion:^(BOOL finished) {
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}];
}];
}
else
{
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Action
- (void)buttonClicked:(id)sender
{
/* Run the button's block */
int buttonIndex = [sender tag] - 1;
[self dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
@end