This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTSMessageView.m
executable file
·360 lines (284 loc) · 12.4 KB
/
TSMessageView.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
//
// TSMessageView.m
// Toursprung
//
// Created by Felix Krause on 24.08.12.
// Copyright (c) 2012 Toursprung. All rights reserved.
//
#import "TSMessageView.h"
#import "HexColor.h"
#define TSMessageViewPadding 15.0
#define TSDesignFileName @"design.json"
static NSMutableDictionary *_notificationDesign;
@interface TSMessageView ()
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSString *buttonTitle;
@property (nonatomic, strong) UIViewController *viewController;
/** Internal properties needed to resize the view on device rotation properly */
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIView *borderView;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, assign) CGFloat textSpaceLeft;
@property (nonatomic, assign) CGFloat textSpaceRight;
@property (copy) void (^callback)();
@property (copy) void (^buttonCallback)();
- (CGFloat)updateHeightOfMessageView;
- (void)layoutSubviews;
@end
@implementation TSMessageView
+ (NSMutableDictionary*)notificationDesign
{
return nil;
}
+ (void)addNotificationDesignFromFile:(NSString*)filename
{
return;
}
- (id)initWithTitle:(NSString *)title
withContent:(NSString *)content
withType:(TSMessageNotificationType)notificationType
withDuration:(CGFloat)duration
inViewController:(UIViewController *)viewController
withCallback:(void (^)())callback
withButtonTitle:(NSString *)buttonTitle
withButtonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)position
shouldBeDismissed:(BOOL)dismissAble
{
if ((self = [self init]))
{
_title = title;
_content = content;
_buttonTitle = buttonTitle;
_duration = duration;
_viewController = viewController;
_messagePosition = position;
self.callback = callback;
self.buttonCallback = buttonCallback;
CGFloat screenWidth = self.viewController.view.bounds.size.width;
NSDictionary *current;
NSString *currentString;
switch (notificationType)
{
case TSMessageNotificationTypeMessage:
{
currentString = @"message";
break;
}
case TSMessageNotificationTypeError:
{
currentString = @"error";
break;
}
case TSMessageNotificationTypeSuccess:
{
currentString = @"success";
break;
}
case TSMessageNotificationTypeWarning:
{
currentString = @"warning";
break;
}
default:
break;
}
self.alpha = 0.0;
UIImage *image;
if ([current valueForKey:@"imageName"])
{
image = [UIImage imageNamed:[current valueForKey:@"imageName"]];
}
self.backgroundColor = [UIColor whiteColor];
UIColor *fontColor = [UIColor colorWithWhite:0.1 alpha:1.0];
self.textSpaceLeft = 2 * TSMessageViewPadding;
if (image) self.textSpaceLeft += image.size.width + 2 * TSMessageViewPadding;
// Set up title label
_titleLabel = [[UILabel alloc] init];
[self.titleLabel setText:title];
[self.titleLabel setTextColor:fontColor];
[self.titleLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self addSubview:self.titleLabel];
// Set up content label (if set)
if ([content length])
{
_contentLabel = [[UILabel alloc] init];
[self.contentLabel setText:content];
[self.contentLabel setTextColor:fontColor];
[self.contentLabel setBackgroundColor:[UIColor clearColor]];
[self.contentLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
self.contentLabel.lineBreakMode = self.titleLabel.lineBreakMode;
self.contentLabel.numberOfLines = 0;
[self addSubview:self.contentLabel];
}
if (image)
{
_iconImageView = [[UIImageView alloc] initWithImage:image];
self.iconImageView.frame = CGRectMake(TSMessageViewPadding * 2,
TSMessageViewPadding,
image.size.width,
image.size.height);
[self addSubview:self.iconImageView];
}
// Set up button (if set)
if ([buttonTitle length])
{
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.button setBackgroundColor:[UIColor colorWithRed:46./255.0 green:144./255.0 blue:90./255.0 alpha:1.0]];
[self.button setTitle:self.buttonTitle forState:UIControlStateNormal];
UIColor *buttonTitleTextColor = [UIColor whiteColor];
[self.button setTitleColor:buttonTitleTextColor forState:UIControlStateNormal];
self.button.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
[self.button addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
self.button.contentEdgeInsets = UIEdgeInsetsMake(0.0, 5.0, 0.0, 5.0);
[self.button sizeToFit];
self.button.frame = CGRectMake(screenWidth - TSMessageViewPadding - self.button.frame.size.width,
0.0,
self.button.frame.size.width,
31.0);
[self addSubview:self.button];
self.textSpaceRight = self.button.frame.size.width + TSMessageViewPadding;
}
// Add a border on the bottom (or on the top, depending on the view's postion)
_borderView = [[UIView alloc] initWithFrame:CGRectMake(0.0,
0.0, // will be set later
screenWidth,
0.5)];
self.borderView.backgroundColor = [UIColor lightGrayColor];
self.borderView.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
[self addSubview:self.borderView];
CGFloat actualHeight = [self updateHeightOfMessageView]; // this call also takes care of positioning the labels
CGFloat topPosition = -actualHeight;
if (self.messagePosition == TSMessageNotificationPositionBottom)
{
topPosition = self.viewController.view.bounds.size.height;
}
self.frame = CGRectMake(0.0, topPosition, screenWidth, actualHeight);
if (self.messagePosition == TSMessageNotificationPositionTop)
{
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
else
{
self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
}
if (dismissAble)
{
UISwipeGestureRecognizer *gestureRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(fadeMeOut)];
[gestureRec setDirection:(self.messagePosition == TSMessageNotificationPositionTop ?
UISwipeGestureRecognizerDirectionUp :
UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:gestureRec];
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(fadeMeOut)];
[self addGestureRecognizer:tapRec];
}
}
return self;
}
- (CGFloat)updateHeightOfMessageView
{
CGFloat currentHeight;
CGFloat screenWidth = self.viewController.view.bounds.size.width;
self.titleLabel.frame = CGRectMake(self.textSpaceLeft,
TSMessageViewPadding,
screenWidth - TSMessageViewPadding - self.textSpaceLeft - self.textSpaceRight,
0.0);
[self.titleLabel sizeToFit];
if ([self.content length])
{
self.contentLabel.frame = CGRectMake(self.textSpaceLeft,
self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 5.0,
screenWidth - TSMessageViewPadding - self.textSpaceLeft - self.textSpaceRight,
0.0);
[self.contentLabel sizeToFit];
currentHeight = self.contentLabel.frame.origin.y + self.contentLabel.frame.size.height;
}
else
{
// only the title was set
currentHeight = self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height;
}
currentHeight += TSMessageViewPadding;
if (self.iconImageView)
{
// Check if that makes the popup larger (height)
if (self.iconImageView.frame.origin.y + self.iconImageView.frame.size.height + TSMessageViewPadding > currentHeight)
{
currentHeight = self.iconImageView.frame.origin.y + self.iconImageView.frame.size.height;
}
else
{
// z-align
self.iconImageView.center = CGPointMake([self.iconImageView center].x,
round(currentHeight / 2.0));
}
}
// z-align button
self.button.center = CGPointMake([self.button center].x,
round(currentHeight / 2.0));
if (self.messagePosition == TSMessageNotificationPositionTop)
{
// Correct the border position
CGRect borderFrame = self.borderView.frame;
borderFrame.origin.y = currentHeight;
self.borderView.frame = borderFrame;
}
currentHeight += self.borderView.frame.size.height;
self.frame = CGRectMake(0.0, self.frame.origin.y, self.frame.size.width, currentHeight);
if (self.button)
{
self.button.frame = CGRectMake(self.frame.size.width - self.textSpaceRight,
round((self.frame.size.height / 2.0) - self.button.frame.size.height / 2.0),
self.button.frame.size.width,
self.button.frame.size.height);
}
self.backgroundImageView.frame = CGRectMake(self.backgroundImageView.frame.origin.x,
self.backgroundImageView.frame.origin.y,
screenWidth,
currentHeight);
return currentHeight;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self updateHeightOfMessageView];
}
- (void)fadeMeOut
{
// user tapped on the message
dispatch_async(dispatch_get_main_queue(), ^
{
if (self.callback)
{
self.callback();
}
[[TSMessage sharedMessage] performSelector:@selector(fadeOutNotification:)
withObject:self];
});
}
#pragma mark - UIButton target
- (void)buttonTapped:(id) sender
{
if (self.buttonCallback)
{
self.buttonCallback();
}
[self fadeMeOut];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ! ([touch.view isKindOfClass:[UIControl class]]);
}
@end