-
Notifications
You must be signed in to change notification settings - Fork 722
/
TSMessageView.m
executable file
路516 lines (420 loc) 路 20.7 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//
// TSMessageView.m
// Felix Krause
//
// Created by Felix Krause on 24.08.12.
// Copyright (c) 2012 Felix Krause. All rights reserved.
//
#import "TSMessageView.h"
#import "HexColor.h"
#import "TSBlurView.h"
#import "TSMessage.h"
#define TSMessageViewPadding 15.0
#define TSDesignFileName @"TSMessagesDefaultDesign.json"
#define TSMessageBundleName @"TSMessages"
static NSMutableDictionary *_notificationDesign;
static NSBundle *podBundle;
@interface TSMessage (TSMessageView)
- (void)fadeOutNotification:(TSMessageView *)currentView; // private method of TSMessage, but called by TSMessageView in -[fadeMeOut]
@end
@interface TSMessageView () <UIGestureRecognizerDelegate>
/** The displayed title of this message */
@property (nonatomic, strong) NSString *title;
/** The displayed subtitle of this message view */
@property (nonatomic, strong) NSString *subtitle;
/** The title of the added button */
@property (nonatomic, strong) NSString *buttonTitle;
/** The view controller this message is displayed in */
@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, strong) TSBlurView *backgroundBlurView; // Only used in iOS 7
@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
{
if (!_notificationDesign)
{
if (!podBundle)
{
podBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:TSMessageBundleName
ofType:@"bundle"]];
}
NSString *path = [[podBundle resourcePath] stringByAppendingPathComponent:TSDesignFileName];
_notificationDesign = [NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path]
options:kNilOptions
error:nil]];
}
return _notificationDesign;
}
+ (void)addNotificationDesignFromFile:(NSString *)filename
{
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSDictionary *design = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path]
options:kNilOptions
error:nil];
[[TSMessageView notificationDesign] addEntriesFromDictionary:design];
}
else
{
NSAssert(NO, @"Error loading design file with name %@", filename);
}
}
- (id)initWithTitle:(NSString *)title
subtitle:(NSString *)subtitle
image:(UIImage *)image
type:(TSMessageNotificationType)notificationType
duration:(CGFloat)duration
inViewController:(UIViewController *)viewController
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)position
canBeDismissedByUser:(BOOL)dismissingEnabled
{
NSDictionary *notificationDesign = [TSMessageView notificationDesign];
if ((self = [self init]))
{
_title = title;
_subtitle = subtitle;
_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;
}
current = [notificationDesign valueForKey:currentString];
if (!image && [[current valueForKey:@"imageName"] length])
{
image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.bundle/%@", TSMessageBundleName, [current valueForKey:@"imageName"]]];
}
if (![TSMessage iOS7StyleEnabled])
{
self.alpha = 0.0;
// add background image here
UIImage *backgroundImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.bundle/%@", TSMessageBundleName, [current valueForKey:@"backgroundImageName"]]];
backgroundImage = [backgroundImage stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
_backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
self.backgroundImageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
[self addSubview:self.backgroundImageView];
}
else
{
// On iOS 7 and above use a blur layer instead (not yet finished)
_backgroundBlurView = [[TSBlurView alloc] init];
self.backgroundBlurView.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
self.backgroundBlurView.blurTintColor = [UIColor colorWithHexString:current[@"backgroundColor"]];
[self addSubview:self.backgroundBlurView];
}
UIColor *fontColor = [UIColor colorWithHexString:[current valueForKey:@"textColor"]
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 setBackgroundColor:[UIColor clearColor]];
CGFloat fontSize = [[current valueForKey:@"titleFontSize"] floatValue];
NSString *fontName = [current valueForKey:@"titleFontName"];
if (fontName != nil) {
[self.titleLabel setFont:[UIFont fontWithName:fontName size:fontSize]];
} else {
[self.titleLabel setFont:[UIFont boldSystemFontOfSize:fontSize]];
}
[self.titleLabel setShadowColor:[UIColor colorWithHexString:[current valueForKey:@"shadowColor"] alpha:1.0]];
[self.titleLabel setShadowOffset:CGSizeMake([[current valueForKey:@"shadowOffsetX"] floatValue],
[[current valueForKey:@"shadowOffsetY"] floatValue])];
self.titleLabel.numberOfLines = 0;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self addSubview:self.titleLabel];
// Set up content label (if set)
if ([subtitle length])
{
_contentLabel = [[UILabel alloc] init];
[self.contentLabel setText:subtitle];
UIColor *contentTextColor = [UIColor colorWithHexString:[current valueForKey:@"contentTextColor"] alpha:1.0];
if (!contentTextColor)
{
contentTextColor = fontColor;
}
[self.contentLabel setTextColor:contentTextColor];
[self.contentLabel setBackgroundColor:[UIColor clearColor]];
CGFloat fontSize = [[current valueForKey:@"contentFontSize"] floatValue];
NSString *fontName = [current valueForKey:@"contentFontName"];
if (fontName != nil) {
[self.contentLabel setFont:[UIFont fontWithName:fontName size:fontSize]];
} else {
[self.contentLabel setFont:[UIFont systemFontOfSize:fontSize]];
}
[self.contentLabel setShadowColor:self.titleLabel.shadowColor];
[self.contentLabel setShadowOffset:self.titleLabel.shadowOffset];
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];
UIImage *buttonBackgroundImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.bundle/%@", TSMessageBundleName, [current valueForKey:@"buttonBackgroundImageName"]]];
buttonBackgroundImage = [buttonBackgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(15.0, 12.0, 15.0, 11.0)];
if (!buttonBackgroundImage)
{
buttonBackgroundImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.bundle/%@", TSMessageBundleName, [current valueForKey:@"NotificationButtonBackground"]]];
buttonBackgroundImage = [buttonBackgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(15.0, 12.0, 15.0, 11.0)];
}
[self.button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
[self.button setTitle:self.buttonTitle forState:UIControlStateNormal];
UIColor *buttonTitleShadowColor = [UIColor colorWithHexString:[current valueForKey:@"buttonTitleShadowColor"] alpha:1.0];
if (!buttonTitleShadowColor)
{
buttonTitleShadowColor = self.titleLabel.shadowColor;
}
[self.button setTitleShadowColor:buttonTitleShadowColor forState:UIControlStateNormal];
UIColor *buttonTitleTextColor = [UIColor colorWithHexString:[current valueForKey:@"buttonTitleTextColor"] alpha:1.0];
if (!buttonTitleTextColor)
{
buttonTitleTextColor = fontColor;
}
[self.button setTitleColor:buttonTitleTextColor forState:UIControlStateNormal];
self.button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
self.button.titleLabel.shadowOffset = CGSizeMake([[current valueForKey:@"buttonTitleShadowOffsetX"] floatValue],
[[current valueForKey:@"buttonTitleShadowOffsetY"] floatValue]);
[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)
if (![TSMessage iOS7StyleEnabled])
{
_borderView = [[UIView alloc] initWithFrame:CGRectMake(0.0,
0.0, // will be set later
screenWidth,
[[current valueForKey:@"borderHeight"] floatValue])];
self.borderView.backgroundColor = [UIColor colorWithHexString:[current valueForKey:@"borderColor"]
alpha:1.0];
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 (dismissingEnabled)
{
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];
}
if (self.callback) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGesture.delegate = self;
[self addGestureRecognizer:tapGesture];
}
}
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.subtitle 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 + TSMessageViewPadding;
}
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);
}
CGRect backgroundFrame = CGRectMake(self.backgroundImageView.frame.origin.x,
self.backgroundImageView.frame.origin.y,
screenWidth,
currentHeight);
// increase frame of background view because of the spring animation
if ([TSMessage iOS7StyleEnabled])
{
if (self.messagePosition == TSMessageNotificationPositionTop)
{
float topOffset = 0.f;
UINavigationController *navigationController = self.viewController.navigationController;
if (!navigationController && [self.viewController isKindOfClass:[UINavigationController class]]) {
navigationController = (UINavigationController *)self.viewController;
}
BOOL isNavBarIsHidden = !navigationController || [TSMessage isNavigationBarInNavigationControllerHidden:navigationController];
BOOL isNavBarIsOpaque = !navigationController.navigationBar.isTranslucent && navigationController.navigationBar.alpha == 1;
if (isNavBarIsHidden || isNavBarIsOpaque) {
topOffset = -30.f;
}
backgroundFrame = UIEdgeInsetsInsetRect(backgroundFrame, UIEdgeInsetsMake(topOffset, 0.f, 0.f, 0.f));
}
else if (self.messagePosition == TSMessageNotificationPositionBottom)
{
backgroundFrame = UIEdgeInsetsInsetRect(backgroundFrame, UIEdgeInsetsMake(0.f, 0.f, -30.f, 0.f));
}
}
self.backgroundImageView.frame = backgroundFrame;
self.backgroundBlurView.frame = backgroundFrame;
return currentHeight;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self updateHeightOfMessageView];
}
- (void)fadeMeOut
{
[[TSMessage sharedMessage] performSelectorOnMainThread:@selector(fadeOutNotification:) withObject:self waitUntilDone:NO];
}
- (void)didMoveToWindow {
[super didMoveToWindow];
if (self.duration == TSMessageNotificationDurationEndless && self.superview && !self.window ) {
// view controller was dismissed, let's fade out
[self fadeMeOut];
}
}
#pragma mark - Target/Action
- (void)buttonTapped:(id) sender
{
if (self.buttonCallback)
{
self.buttonCallback();
}
[self fadeMeOut];
}
- (void)handleTap:(UITapGestureRecognizer *)tapGesture
{
if (tapGesture.state == UIGestureRecognizerStateRecognized)
{
if (self.callback)
{
self.callback();
}
}
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ! ([touch.view isKindOfClass:[UIControl class]]);
}
@end