-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUIAlertView.m
executable file
·319 lines (278 loc) · 13.1 KB
/
FUIAlertView.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
//
// FUIAlertView.m
// FlatUI
//
// Created by Jack Flintermann on 5/7/13.
// Copyright (c) 2013 Jack Flintermann. All rights reserved.
//
#import "FUIAlertView.h"
#import "FUIButton.h"
@interface FUIAlertView()
@property(nonatomic, weak) UIView *alertContentContainer;
@end
@implementation FUIAlertView
+ (void)initialize {
if (self == [FUIAlertView class]) {
FUIAlertView *appearance = [self appearance];
[appearance setButtonSpacing:10.0f];
[appearance setAnimationDuration:0.2f];
}
}
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id<FUIAlertViewDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithFrame:CGRectZero];
if (self) {
self.title = title;
self.message = message;
self.delegate = delegate;
// This mask is set to force lay out of subviews when superview's bounds change
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIView *backgroundOverlay = [[UIView alloc] init];
backgroundOverlay.backgroundColor = [UIColor blueColor];
[self addSubview:backgroundOverlay];
backgroundOverlay.alpha = 0;
_backgroundOverlay = backgroundOverlay;
UIView *alertContainer = [[UIView alloc] init];
alertContainer.backgroundColor = [UIColor yellowColor];
[self addSubview:alertContainer];
[self bringSubviewToFront:alertContainer];
_alertContainer = alertContainer;
UIView *alertContentContainer = [[UIView alloc] init];
alertContentContainer.backgroundColor = [UIColor clearColor];
[self.alertContainer addSubview:alertContentContainer];
[self.alertContainer bringSubviewToFront:alertContentContainer];
_alertContentContainer = alertContentContainer;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = 0;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = self.title;
[alertContentContainer addSubview:titleLabel];
_titleLabel = titleLabel;
UILabel *messageLabel = [[UILabel alloc] init];
messageLabel.numberOfLines = 0;
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.text = self.message;
[alertContentContainer addSubview:messageLabel];
_messageLabel = messageLabel;
if (cancelButtonTitle) {
[self addButtonWithTitle:cancelButtonTitle];
[self setHasCancelButton:YES];
}
va_list args;
va_start(args, otherButtonTitles);
for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*)) {
[self addButtonWithTitle:arg];
}
va_end(args);
}
return self;
}
- (void) layoutSubviews {
[super layoutSubviews];
if (CGAffineTransformIsIdentity(self.alertContainer.transform)) {
self.backgroundOverlay.frame = self.bounds;
CGFloat padding = 15;
CGRect contentContainerFrame = CGRectMake(padding, padding, 0, 0);
contentContainerFrame.size = [self calculateSize];
self.alertContentContainer.frame = contentContainerFrame;
CGRect alertContainerFrame = CGRectInset(contentContainerFrame, -padding, -padding);
alertContainerFrame.origin = CGPointMake(floorf((self.frame.size.width - alertContainerFrame.size.width) / 2),
floorf((self.frame.size.height - alertContainerFrame.size.height) / 2));
alertContainerFrame.origin.y = MAX(30, alertContainerFrame.origin.y - 30);
self.alertContainer.frame = alertContainerFrame;
CGRect titleFrame = self.titleLabel.frame;
titleFrame.size.width = self.alertContentContainer.frame.size.width;
self.titleLabel.frame = titleFrame;
[self.titleLabel sizeToFit];
titleFrame = self.titleLabel.frame;
CGPoint titleOrigin = CGPointMake(floorf((self.alertContentContainer.frame.size.width - self.titleLabel.frame.size.width)/2), 0);
titleFrame.origin = titleOrigin;
self.titleLabel.frame = titleFrame;
CGRect messageFrame = self.messageLabel.frame;
messageFrame.size.width = self.alertContentContainer.frame.size.width;
self.messageLabel.frame = messageFrame;
[self.messageLabel sizeToFit];
messageFrame = self.messageLabel.frame;
CGPoint messageOrigin = CGPointMake(floorf((self.alertContentContainer.frame.size.width - self.messageLabel.frame.size.width)/2), CGRectGetMaxY(titleFrame) + 10);
messageFrame.origin = messageOrigin;
self.messageLabel.frame = messageFrame;
__block CGFloat startingButtonY = self.alertContentContainer.frame.size.height - [self totalButtonHeight];
[self.buttons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIButton *button = obj;
if (self.hasCancelButton && idx == 0) {
CGFloat lastButtonY = self.alertContentContainer.frame.size.height - button.frame.size.height;
[self setButton:obj atHeight:lastButtonY];
} else {
[self setButton:obj atHeight:startingButtonY];
startingButtonY += (button.frame.size.height + self.buttonSpacing);
}
}];
if(self.messageLabel.superview&&![self.messageLabel.superview isEqual:self.alertContentContainer]) {
[self.messageLabel removeFromSuperview];
[self.alertContentContainer addSubview:self.messageLabel];
}
if(self.maxHeight) {
CGSize originalSize = messageFrame.size;
messageFrame.size.height = self.alertContentContainer.frame.size.height-self.titleLabel.frame.size.height-[self totalButtonHeight]-20;
if(messageFrame.size.height<originalSize.height) {
UIScrollView *messageScrollView = [[UIScrollView alloc] initWithFrame:messageFrame];
messageFrame.origin = CGPointZero;
messageFrame.size = originalSize;
self.messageLabel.frame = messageFrame;
[messageScrollView setContentSize:originalSize];
[messageScrollView addSubview:self.messageLabel];
[self.alertContentContainer addSubview:messageScrollView];
}
}
}
}
- (void)setButton:(UIButton *)button atHeight:(CGFloat)height {
CGRect buttonFrame = button.frame;
buttonFrame.origin = CGPointMake(0, height);
buttonFrame.size.width = self.alertContentContainer.frame.size.width;
button.frame = buttonFrame;
}
- (CGFloat) totalButtonHeight {
__block CGFloat buttonHeight = 0;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
buttonHeight += (button.frame.size.height + self.buttonSpacing);
}];
buttonHeight -= self.buttonSpacing;
return buttonHeight;
}
- (CGSize) calculateSize {
CGFloat contentWidth = 250;
CGFloat titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(contentWidth, CGFLOAT_MAX)].height;
CGFloat messageHeight = [self.messageLabel.text sizeWithFont:self.messageLabel.font constrainedToSize:CGSizeMake(contentWidth, CGFLOAT_MAX)].height;
CGFloat buttonHeight = [self totalButtonHeight];
return CGSizeMake(contentWidth, titleHeight + 10 + messageHeight + 10 + buttonHeight);
}
- (NSInteger) numberOfButtons {
return (NSInteger)self.buttons.count;
}
- (void)show {
self.alertContainer.alpha = 0;
self.alertContainer.transform = CGAffineTransformMakeScale(1.3, 1.3);
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController && !topController.presentedViewController.isBeingDismissed) {
topController = topController.presentedViewController;
}
UIView *rootView = topController.view;
self.frame = rootView.bounds;
[rootView addSubview:self];
[rootView bringSubviewToFront:self];
if ([self.delegate respondsToSelector:@selector(willPresentAlertView:)]) {
[self.delegate willPresentAlertView:self];
}
[UIView animateWithDuration:self.animationDuration animations:^{
self.backgroundOverlay.alpha = 1;
self.alertContainer.alpha = 1;
self.alertContainer.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished0) {
_visible = YES;
if ([self.delegate respondsToSelector:@selector(didPresentAlertView:)]) {
[self.delegate didPresentAlertView:self];
}
}];
}
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex {
if (buttonIndex < 0 || buttonIndex > (NSInteger)self.buttons.count) {
return nil;
}
return [[self.buttons objectAtIndex:(NSUInteger)buttonIndex] titleForState:UIControlStateNormal];
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated {
//todo delegate
_dismissButtonIndex = buttonIndex;
self.alertContainer.transform = CGAffineTransformIdentity;
if ([self.delegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)]) {
[self.delegate alertView:self willDismissWithButtonIndex:buttonIndex];
}
if (self.onDismissAction) {
self.onDismissAction();
}
[UIView animateWithDuration:self.animationDuration animations:^{
self.backgroundOverlay.alpha = 0;
self.alertContainer.alpha = 0;
self.alertContainer.transform = CGAffineTransformMakeScale(0.7, 0.7);
} completion:^(BOOL finished) {
[self removeFromSuperview];
_visible = NO;
if ([self.delegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)]) {
[self.delegate alertView:self didDismissWithButtonIndex:buttonIndex];
}
}];
}
- (NSInteger)addButtonWithTitle:(NSString *)title {
if (!title) return -1;
if (!self.buttons) {
self.buttons = [NSMutableArray array];
}
FUIButton *button = [[FUIButton alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
button.cornerRadius = 3;
button.buttonColor = [UIColor greenColor];
button.shadowColor = [UIColor brownColor];
button.shadowHeight = 3;
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.alertContentContainer addSubview:button];
[self.buttons addObject:button];
return (NSInteger)self.buttons.count-1;
}
- (void) buttonPressed:(FUIButton *)sender {
NSUInteger index = [self.buttons indexOfObject:sender];
if ([self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
[self.delegate alertView:self clickedButtonAtIndex:(NSInteger)index];
}
if (index == self.cancelButtonIndex && self.onCancelAction) {
self.onCancelAction();
} else if (index != self.cancelButtonIndex && self.onOkAction) {
self.onOkAction();
}
[self dismissWithClickedButtonIndex:(NSInteger)index animated:YES];
}
- (void)clickButtonAtIndex:(NSInteger)buttonIndex {
[[self.buttons objectAtIndex:(NSUInteger)buttonIndex] sendActionsForControlEvents:UIControlEventTouchUpInside];
}
- (void) setDefaultButtonFont:(UIFont *)defaultButtonFont {
_defaultButtonFont = defaultButtonFont;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
button.titleLabel.font = defaultButtonFont;
}];
}
- (void) setDefaultButtonTitleColor:(UIColor *)defaultButtonTitleColor {
_defaultButtonTitleColor = defaultButtonTitleColor;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
[button setTitleColor:defaultButtonTitleColor forState:UIControlStateNormal & UIControlStateHighlighted];
}];
}
- (void) setDefaultButtonColor:(UIColor *)defaultButtonColor {
_defaultButtonColor = defaultButtonColor;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
button.buttonColor = defaultButtonColor;
}];
}
- (void) setDefaultButtonShadowColor:(UIColor *)defaultButtonShadowColor {
_defaultButtonShadowColor = defaultButtonShadowColor;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
button.shadowColor = defaultButtonShadowColor;
}];
}
- (void) setDefaultButtonCornerRadius:(CGFloat)defaultButtonCornerRadius {
_defaultButtonCornerRadius = defaultButtonCornerRadius;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
button.cornerRadius = defaultButtonCornerRadius;
}];
}
- (void) setDefaultButtonShadowHeight:(CGFloat)defaultButtonShadowHeight {
_defaultButtonShadowHeight = defaultButtonShadowHeight;
[self.buttons enumerateObjectsUsingBlock:^(FUIButton *button, NSUInteger idx, BOOL *stop) {
button.shadowHeight = defaultButtonShadowHeight;
}];
}
@end