-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKJDropdownAlert.m
More file actions
executable file
·187 lines (152 loc) · 6.68 KB
/
KJDropdownAlert.m
File metadata and controls
executable file
·187 lines (152 loc) · 6.68 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
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
//
// KJDropdownAlert.m
// KJDropdownAlert
//
// Created by Richard Kim on 8/26/14.
// Copyright (c) 2014 Richard Kim. All rights reserved.
// Created by Kurt Jensen on 7/29/15.
// Copyright (c) 2015 Kurt Jensen. All rights reserved.
//
#import "KJDropdownAlert.h"
NSString *const KJDropdownAlertDismissAllNotification = @"KJDropdownAlertDismissAllNotification";
static int HEIGHT = 90; //height of the alert view
static float ANIMATION_TIME = .3; //time it takes for the animation to complete in seconds
static int X_BUFFER = 10; //buffer distance on each side for the text
static int Y_BUFFER = 10; //buffer distance on top/bottom for the text
static int STATUS_BAR_HEIGHT = 20;
@implementation KJDropdownAlert{
UILabel *titleLabel;
UILabel *messageLabel;
}
- (id)initWithFrame:(CGRect)frame titleFont:(UIFont *)titleFont subTitleFont:(UIFont *)subTitleFont
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
//%%% title setup (the bolded text at the top of the view)
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(X_BUFFER, STATUS_BAR_HEIGHT, frame.size.width-2*X_BUFFER, 30)];
[titleLabel setFont:titleFont];
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:titleLabel];
//%%% message setup (the regular text below the title)
messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(X_BUFFER, STATUS_BAR_HEIGHT +Y_BUFFER*2.3, frame.size.width-2*X_BUFFER, 40)];
messageLabel.textColor = [UIColor blackColor];
messageLabel.font = subTitleFont;
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
messageLabel.numberOfLines = 2; // 2 lines ; 0 - dynamic number of lines
messageLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:messageLabel];
[self addTarget:self action:@selector(hideView:) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dismissAlertView)
name:KJDropdownAlertDismissAllNotification
object:nil];
self.isShowing = NO;
}
return self;
}
- (void)dismissAlertView {
[self hideView:self];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:KJDropdownAlertDismissAllNotification
object:nil];
}
//%%% button method (what happens when you touch the drop down view)
-(void)viewWasTapped:(UIButton *)alertView
{
[self hideView:alertView];
}
-(void)hideView:(UIButton *)alertView
{
if (alertView) {
[UIView animateWithDuration:ANIMATION_TIME animations:^{
CGRect frame = alertView.frame;
frame.origin.y = -HEIGHT;
alertView.frame = frame;
}];
[self performSelector:@selector(removeView:) withObject:alertView afterDelay:ANIMATION_TIME];
}
}
-(void)removeView:(KJDropdownAlert *)alertView
{
if (alertView){
[alertView removeFromSuperview];
self.isShowing = NO;
if (self.delegate){
[self.delegate didDismissDropdownAlert:alertView];
}
}
}
#pragma mark IGNORE THESE
//%%% these are necessary methods that call each other depending on which method you call. Generally shouldn't edit these unless you know what you're doing
+(KJDropdownAlert*)alertViewWithDelegate:(id<KJDropdownAlertDelegate>)delegate numberOfAlertsShowing:(NSInteger)numberShowing titleFont:(UIFont *)titleFont subTitleFont:(UIFont *)subTitleFont
{
KJDropdownAlert *alert = [[self alloc]initWithFrame:CGRectMake(0, (HEIGHT*numberShowing)-HEIGHT, [[UIScreen mainScreen]bounds].size.width, HEIGHT) titleFont:titleFont subTitleFont:subTitleFont];
alert.delegate = delegate;
return alert;
}
/* reserved */
+(void)title:(NSString*)title message:(NSString*)message backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor time:(NSInteger)seconds delegate:(id<KJDropdownAlertDelegate>)delegate titleFont:(UIFont *)titleFont subTitleFont:(UIFont *)subTitleFont numberOfAlertsShowing:(NSInteger)numberShowing
{
[[self alertViewWithDelegate:delegate numberOfAlertsShowing:numberShowing titleFont:titleFont subTitleFont:subTitleFont] title:title message:message backgroundColor:backgroundColor textColor:textColor time:seconds numberShowing:numberShowing];
}
+(void)dismissAllAlert{
[[NSNotificationCenter defaultCenter] postNotificationName:KJDropdownAlertDismissAllNotification object:nil];
}
-(void)title:(NSString*)title message:(NSString*)message backgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor time:(NSInteger)seconds numberShowing:(NSInteger)numberShowing
{
NSInteger time = seconds;
titleLabel.text = title;
if (message && message.length > 0) {
messageLabel.text = message;
if ([self messageTextIsOneLine]) {
CGRect frame = titleLabel.frame;
frame.origin.y = STATUS_BAR_HEIGHT+5;
titleLabel.frame = frame;
}
} else {
CGRect frame = titleLabel.frame;
frame.size.height = HEIGHT-2*Y_BUFFER-STATUS_BAR_HEIGHT;
frame.origin.y = Y_BUFFER+STATUS_BAR_HEIGHT;
titleLabel.frame = frame;
}
if (backgroundColor) {
self.backgroundColor = backgroundColor;
}
if (textColor) {
titleLabel.textColor = textColor;
messageLabel.textColor = textColor;
}
if(!self.superview){
NSEnumerator *frontToBackWindows = [[[UIApplication sharedApplication]windows]reverseObjectEnumerator];
for (UIWindow *window in frontToBackWindows)
if (window.windowLevel == UIWindowLevelNormal && !window.hidden) {
[window addSubview:self];
break;
}
}
self.isShowing = YES;
[UIView animateWithDuration:ANIMATION_TIME animations:^{
CGRect frame = self.frame;
frame.origin.y = HEIGHT*numberShowing;
self.frame = frame;
}];
if (self.delegate) {
[self.delegate willShowDropdownAlert:self];
}
[self performSelector:@selector(viewWasTapped:) withObject:self afterDelay:time+ANIMATION_TIME];
}
-(BOOL)messageTextIsOneLine
{
CGSize size = [messageLabel.text sizeWithAttributes:
@{NSFontAttributeName:
messageLabel.font}];
if (size.width > messageLabel.frame.size.width) {
return NO;
}
return YES;
}
@end