forked from rayyan/CODialog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCODialog.m
907 lines (713 loc) · 31.2 KB
/
CODialog.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
//
// CODialog.m
// CODialog
//
// Created by Erik Aigner on 10.04.12.
// Copyright (c) 2012 chocomoko.com. All rights reserved.
//
#import "CODialog.h"
@interface CODialogTextField : UITextField
@property (nonatomic, strong) CODialog *dialog;
@end
@interface CODialogWindowOverlay : UIWindow
@property (nonatomic, strong) CODialog *dialog;
@end
@interface CODialog ()
@property (nonatomic, strong) CODialogWindowOverlay *overlay;
@property (nonatomic, strong) UIWindow *hostWindow;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *accessoryView;
@property (nonatomic, strong) NSMutableArray *textFields;
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, strong) UIFont *titleFont;
@property (nonatomic, strong) UIFont *subtitleFont;
@property (nonatomic, assign) NSInteger highlightedIndex;
@end
#define CODialogSynth(x) @synthesize x = x##_;
#define CODialogAssertMQ() NSAssert(dispatch_get_current_queue() == dispatch_get_main_queue(), @"%@ must be called on main queue", NSStringFromSelector(_cmd));
#define kCODialogAnimationDuration 0.15
#define kCODialogPopScale 0.5
#define kCODialogPadding 8.0
#define kCODialogFrameInset 8.0
#define kCODialogButtonHeight 44.0
#define kCODialogTextFieldHeight 29.0
@implementation CODialog {
@private
struct {
CGRect titleRect;
CGRect subtitleRect;
CGRect accessoryRect;
CGRect textFieldsRect;
CGRect buttonRect;
} layout;
}
CODialogSynth(customView)
CODialogSynth(dialogStyle)
CODialogSynth(title)
CODialogSynth(subtitle)
CODialogSynth(batchDelay)
CODialogSynth(overlay)
CODialogSynth(hostWindow)
CODialogSynth(contentView)
CODialogSynth(accessoryView)
CODialogSynth(textFields)
CODialogSynth(buttons)
CODialogSynth(titleFont)
CODialogSynth(subtitleFont)
CODialogSynth(highlightedIndex)
+ (instancetype)dialogWithWindow:(UIWindow *)hostWindow {
return [[self alloc] initWithWindow:hostWindow];
}
- (id)initWithWindow:(UIWindow *)hostWindow {
self = [super initWithFrame:[self defaultDialogFrame]];
if (self) {
self.batchDelay = 0;
self.highlightedIndex = -1;
self.titleFont = [UIFont boldSystemFontOfSize:18.0];
self.subtitleFont = [UIFont systemFontOfSize:14.0];
self.hostWindow = hostWindow;
self.opaque = NO;
self.alpha = 1.0;
self.buttons = [NSMutableArray new];
self.textFields = [NSMutableArray new];
// Register for keyboard notifications
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)adjustToKeyboardBounds:(CGRect)bounds {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat height = CGRectGetHeight(screenBounds) - CGRectGetHeight(bounds);
CGRect frame = self.frame;
frame.origin.y = (height - CGRectGetHeight(self.bounds)) / 2.0;
if (CGRectGetMinY(frame) < 0) {
NSLog(@"warning: dialog is clipped, origin negative (%f)", CGRectGetMinY(frame));
}
[UIView animateWithDuration:kCODialogAnimationDuration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.frame = frame;
} completion:^(BOOL finished) {
// stub
}];
}
- (void)keyboardWillShow:(NSNotification *)note {
NSValue *value = [[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect frame = [value CGRectValue];
[self adjustToKeyboardBounds:frame];
}
- (void)keyboardWillHide:(NSNotification *)note {
[self adjustToKeyboardBounds:CGRectZero];
}
- (CGRect)defaultDialogFrame {
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
CGRect insetFrame = CGRectIntegral(CGRectInset(appFrame, 20.0, 20.0));
insetFrame.size.height = 180.0;
return insetFrame;
}
- (void)setProgress:(CGFloat)progress {
UIProgressView *view = (id)self.accessoryView;
if ([view isKindOfClass:[UIProgressView class]]) {
[view setProgress:progress animated:YES];
}
}
- (CGFloat)progress {
UIProgressView *view = (id)self.accessoryView;
if ([view isKindOfClass:[UIProgressView class]]) {
return view.progress;
}
return 0;
}
- (UIView *)makeAccessoryView {
if (self.dialogStyle == CODialogStyleIndeterminate) {
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityView startAnimating];
return activityView;
} else if (self.dialogStyle == CODialogStyleDeterminate) {
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.frame = CGRectMake(0, 0, 200.0, 88.0);
return progressView;
} else if (self.dialogStyle == CODialogStyleSuccess ||
self.dialogStyle == CODialogStyleError) {
CGSize iconSize = CGSizeMake(64, 64);
UIGraphicsBeginImageContextWithOptions(iconSize, NO, 0);
[self drawSymbolInRect:(CGRect){CGPointZero, iconSize}];
UIImageView *imageView = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
return imageView;
} else if (self.dialogStyle == CODialogStyleCustomView) {
return self.customView;
}
return nil;
}
- (void)layoutComponents {
[self setNeedsDisplay];
// Compute frames of components
CGFloat layoutFrameInset = kCODialogFrameInset + kCODialogPadding;
CGRect layoutFrame = CGRectInset(self.bounds, layoutFrameInset, layoutFrameInset);
CGFloat layoutWidth = CGRectGetWidth(layoutFrame);
// Title frame
CGFloat titleHeight = 0;
CGFloat minY = CGRectGetMinY(layoutFrame);
if (self.title.length > 0) {
titleHeight = [self.title sizeWithFont:self.titleFont
constrainedToSize:CGSizeMake(layoutWidth, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap].height;
minY += kCODialogPadding;
}
layout.titleRect = CGRectMake(CGRectGetMinX(layoutFrame), minY, layoutWidth, titleHeight);
// Subtitle frame
CGFloat subtitleHeight = 0;
minY = CGRectGetMaxY(layout.titleRect);
if (self.subtitle.length > 0) {
subtitleHeight = [self.subtitle sizeWithFont:self.subtitleFont
constrainedToSize:CGSizeMake(layoutWidth, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap].height;
minY += kCODialogPadding;
}
layout.subtitleRect = CGRectMake(CGRectGetMinX(layoutFrame), minY, layoutWidth, subtitleHeight);
// Accessory frame (note that views are in the content view coordinate system)
self.accessoryView = [self makeAccessoryView];
self.accessoryView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
CGFloat accessoryHeight = 0;
CGFloat accessoryWidth = CGRectGetWidth(layoutFrame);
CGFloat accessoryLeft = 0;
minY = CGRectGetMaxY(layout.subtitleRect) - layoutFrameInset;
if (self.accessoryView != nil) {
accessoryHeight = CGRectGetHeight(self.accessoryView.frame);
accessoryWidth = CGRectGetWidth(self.accessoryView.frame);
accessoryLeft = (CGRectGetWidth(layoutFrame) - accessoryWidth) / 2.0;
minY += kCODialogPadding;
}
layout.accessoryRect = CGRectMake(accessoryLeft, minY, accessoryWidth, accessoryHeight);
// Text fields frame (note that views are in the content view coordinate system)
CGFloat textFieldsHeight = 0;
NSUInteger numTextFields = self.textFields.count;
minY = CGRectGetMaxY(layout.accessoryRect);
if (numTextFields > 0) {
textFieldsHeight = kCODialogTextFieldHeight * (CGFloat)numTextFields + kCODialogPadding * ((CGFloat)numTextFields - 1.0);
minY += kCODialogPadding;
}
layout.textFieldsRect = CGRectMake(CGRectGetMinX(layoutFrame), minY, layoutWidth, textFieldsHeight);
// Buttons frame (note that views are in the content view coordinate system)
CGFloat buttonsHeight = 0;
minY = CGRectGetMaxY(layout.textFieldsRect);
if (self.buttons.count > 0) {
buttonsHeight = kCODialogButtonHeight;
minY += kCODialogPadding;
}
layout.buttonRect = CGRectMake(CGRectGetMinX(layoutFrame), minY, layoutWidth, buttonsHeight);
// Adjust layout frame
layoutFrame.size.height = CGRectGetMaxY(layout.buttonRect);
// Create new content view
UIView *newContentView = [[UIView alloc] initWithFrame:layoutFrame];
newContentView.contentMode = UIViewContentModeRedraw;
// Layout accessory view
self.accessoryView.frame = layout.accessoryRect;
[newContentView addSubview:self.accessoryView];
// Layout text fields
if (numTextFields > 0) {
for (int i=0; i<numTextFields; i++) {
CGFloat offsetY = (kCODialogTextFieldHeight + kCODialogPadding) * (CGFloat)i;
CGRect fieldFrame = CGRectMake(0,
CGRectGetMinY(layout.textFieldsRect) + offsetY,
layoutWidth,
kCODialogTextFieldHeight);
UITextField *field = [self.textFields objectAtIndex:i];
field.frame = fieldFrame;
field.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
[newContentView addSubview:field];
}
}
// Layout buttons
NSUInteger count = self.buttons.count;
if (count > 0) {
CGFloat buttonWidth = (CGRectGetWidth(layout.buttonRect) - kCODialogPadding * ((CGFloat)count - 1.0)) / (CGFloat)count;
for (int i=0; i<count; i++) {
CGFloat left = (kCODialogPadding + buttonWidth) * (CGFloat)i;
CGRect buttonFrame = CGRectIntegral(CGRectMake(left, CGRectGetMinY(layout.buttonRect), buttonWidth, CGRectGetHeight(layout.buttonRect)));
UIButton *button = [self.buttons objectAtIndex:i];
button.frame = buttonFrame;
button.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
BOOL highlighted = (self.highlightedIndex == i);
NSString *title = [button titleForState:UIControlStateNormal];
// Set default image
UIGraphicsBeginImageContextWithOptions(buttonFrame.size, NO, 0);
[self drawButtonInRect:(CGRect){CGPointZero, buttonFrame.size} title:title highlighted:highlighted down:NO];
[button setImage:UIGraphicsGetImageFromCurrentImageContext() forState:UIControlStateNormal];
UIGraphicsEndImageContext();
// Set alternate image
UIGraphicsBeginImageContextWithOptions(buttonFrame.size, NO, 0);
[self drawButtonInRect:(CGRect){CGPointZero, buttonFrame.size} title:title highlighted:NO down:YES];
[button setImage:UIGraphicsGetImageFromCurrentImageContext() forState:UIControlStateHighlighted];
UIGraphicsEndImageContext();
[newContentView addSubview:button];
}
}
// Fade content views
CGFloat animationDuration = kCODialogAnimationDuration;
if (self.contentView.superview != nil) {
[UIView transitionFromView:self.contentView
toView:newContentView
duration:animationDuration
options:UIViewAnimationOptionTransitionCrossDissolve
completion:^(BOOL finished) {
self.contentView = newContentView;
}];
} else {
self.contentView = newContentView;
[self addSubview:newContentView];
// Don't animate frame adjust if there was no content before
animationDuration = 0;
}
// Adjust frame size
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
CGRect dialogFrame = CGRectInset(layoutFrame, -kCODialogFrameInset - kCODialogPadding, -kCODialogFrameInset - kCODialogPadding);
dialogFrame.origin.x = (CGRectGetWidth(self.hostWindow.bounds) - CGRectGetWidth(dialogFrame)) / 2.0;
dialogFrame.origin.y = (CGRectGetHeight(self.hostWindow.bounds) - CGRectGetHeight(dialogFrame)) / 2.0;
self.frame = CGRectIntegral(dialogFrame);
} completion:^(BOOL finished) {
[self setNeedsDisplay];
}];
}
- (void)resetLayout {
self.title = nil;
self.subtitle = nil;
self.dialogStyle = CODialogStyleDefault;
self.progress = 0;
self.customView = nil;
[self removeAllControls];
}
- (void)removeAllControls {
[self removeAllTextFields];
[self removeAllButtons];
}
- (void)removeAllTextFields {
[self.textFields removeAllObjects];
}
- (void)removeAllButtons {
[self.buttons removeAllObjects];
self.highlightedIndex = -1;
}
- (void)addTextFieldWithPlaceholder:(NSString *)placeholder secure:(BOOL)secure {
for (UITextField *field in self.textFields) {
field.returnKeyType = UIReturnKeyNext;
}
CODialogTextField *field = [[CODialogTextField alloc] initWithFrame:CGRectMake(0, 0, 200, kCODialogTextFieldHeight)];
field.dialog = self;
field.returnKeyType = UIReturnKeyDone;
field.placeholder = placeholder;
field.secureTextEntry = secure;
field.font = [UIFont systemFontOfSize:kCODialogTextFieldHeight - 8.0];
field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
field.textColor = [UIColor blackColor];
field.keyboardAppearance = UIKeyboardAppearanceAlert;
field.delegate = (id)self;
[self.textFields addObject:field];
}
- (void)addButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)sel {
[self addButtonWithTitle:title target:target selector:sel highlighted:NO];
}
- (void)addButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)sel highlighted:(BOOL)flag {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
if (flag) {
self.highlightedIndex = self.buttons.count;
}
[self.buttons addObject:button];
}
- (NSString *)textForTextFieldAtIndex:(NSUInteger)index {
UITextField *field = [self.textFields objectAtIndex:index];
return [field text];
}
- (void)showOrUpdateAnimatedInternal:(BOOL)flag {
CODialogAssertMQ();
CODialogWindowOverlay *overlay = self.overlay;
BOOL show = (overlay == nil);
// Create overlay
if (show) {
self.overlay = overlay = [CODialogWindowOverlay new];
overlay.opaque = NO;
overlay.windowLevel = UIWindowLevelStatusBar + 1;
overlay.dialog = self;
overlay.frame = self.hostWindow.bounds;
overlay.alpha = 0.0;
}
// Layout components
[self layoutComponents];
if (show) {
// Scale down ourselves for pop animation
self.transform = CGAffineTransformMakeScale(kCODialogPopScale, kCODialogPopScale);
// Animate
NSTimeInterval animationDuration = (flag ? kCODialogAnimationDuration : 0.0);
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
overlay.alpha = 1.0;
self.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
// stub
}];
[overlay addSubview:self];
[overlay makeKeyAndVisible];
}
}
- (void)showOrUpdateAnimated:(BOOL)flag {
CODialogAssertMQ();
SEL selector = @selector(showOrUpdateAnimatedInternal:);
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil];
[self performSelector:selector withObject:[NSNumber numberWithBool:flag] afterDelay:self.batchDelay];
}
- (void)hideAnimated:(BOOL)flag {
CODialogAssertMQ();
CODialogWindowOverlay *overlay = self.overlay;
// Nothing to hide if it is not key window
if (overlay == nil) {
return;
}
NSTimeInterval animationDuration = (flag ? kCODialogAnimationDuration : 0.0);
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
overlay.alpha = 0.0;
self.transform = CGAffineTransformMakeScale(kCODialogPopScale, kCODialogPopScale);
} completion:^(BOOL finished) {
overlay.hidden = YES;
self.transform = CGAffineTransformIdentity;
[self removeFromSuperview];
self.overlay = nil;
}];
}
- (void)hideAnimated:(BOOL)flag afterDelay:(NSTimeInterval)delay {
CODialogAssertMQ();
SEL selector = @selector(hideAnimated:);
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil];
[self performSelector:selector withObject:[NSNumber numberWithBool:flag] afterDelay:delay];
}
- (void)drawDialogBackgroundInRect:(CGRect)rect {
// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
// Set alpha
CGContextSaveGState(context);
CGContextSetAlpha(context, 0.65);
// Color Declarations
UIColor *color = [UIColor colorWithRed:0.047 green:0.141 blue:0.329 alpha:1.0];
// Gradient Declarations
NSArray *gradientColors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0 alpha:0.75].CGColor,
(id)[UIColor colorWithRed:0.227 green:0.310 blue:0.455 alpha:0.8].CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient2 = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
// Abstracted Graphic Attributes
CGFloat cornerRadius = 8.0;
CGFloat strokeWidth = 2.0;
CGColorRef dialogShadow = [UIColor blackColor].CGColor;
CGSize shadowOffset = CGSizeMake(0, 4);
CGFloat shadowBlurRadius = kCODialogFrameInset - 2.0;
CGRect frame = CGRectInset(CGRectIntegral(self.bounds), kCODialogFrameInset, kCODialogFrameInset);
// Rounded Rectangle Drawing
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:cornerRadius];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, dialogShadow);
[color setFill];
[roundedRectanglePath fill];
CGContextRestoreGState(context);
// Set clip path
[roundedRectanglePath addClip];
// Bezier Drawing
CGFloat mx = CGRectGetMinX(frame);
CGFloat my = CGRectGetMinY(frame);
CGFloat w = CGRectGetWidth(frame);
CGFloat w2 = w / 2.0;
CGFloat w4 = w / 4.0;
CGFloat h1 = 25;
CGFloat h2 = 35;
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(mx, h1)];
[bezierPath addCurveToPoint:CGPointMake(mx + w2, h2) controlPoint1:CGPointMake(mx, h1) controlPoint2:CGPointMake(mx + w4, h2)];
[bezierPath addCurveToPoint:CGPointMake(mx + w, h1) controlPoint1:CGPointMake(mx + w2 + w4, h2) controlPoint2:CGPointMake(mx + w, h1)];
[bezierPath addCurveToPoint:CGPointMake(mx + w, my) controlPoint1:CGPointMake(mx + w, h1) controlPoint2:CGPointMake(mx + w, my)];
[bezierPath addCurveToPoint:CGPointMake(mx, my) controlPoint1:CGPointMake(mx + w, my) controlPoint2:CGPointMake(mx, my)];
[bezierPath addLineToPoint:CGPointMake(mx, h1)];
[bezierPath closePath];
CGContextSaveGState(context);
[bezierPath addClip];
CGContextDrawLinearGradient(context, gradient2, CGPointMake(w2, 0), CGPointMake(w2, h2), 0);
CGContextRestoreGState(context);
// Stroke
[[UIColor whiteColor] setStroke];
UIBezierPath *strokePath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(frame, strokeWidth / 2.0, strokeWidth / 2.0)
cornerRadius:cornerRadius - strokeWidth / 2.0];
strokePath.lineWidth = strokeWidth;
[strokePath stroke];
// Cleanup
CGGradientRelease(gradient2);
CGColorSpaceRelease(colorSpace);
CGContextRestoreGState(context);
}
- (void)drawButtonInRect:(CGRect)rect title:(NSString *)title highlighted:(BOOL)highlighted down:(BOOL)down {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGFloat radius = 4.0;
CGFloat strokeWidth = 1.0;
CGRect frame = CGRectIntegral(rect);
CGRect buttonFrame = CGRectInset(frame, 0, 1);
// Color declarations
UIColor* whiteTop = [UIColor colorWithWhite:1.0 alpha:0.35];
UIColor* whiteMiddle = [UIColor colorWithWhite:1.0 alpha:0.10];
UIColor* whiteBottom = [UIColor colorWithWhite:1.0 alpha:0.0];
// Gradient declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)whiteTop.CGColor,
(id)whiteMiddle.CGColor,
(id)whiteBottom.CGColor,
(id)whiteBottom.CGColor, nil];
CGFloat gradientLocations[] = {0, 0.5, 0.5, 1};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
CGColorSpaceRelease(colorSpace);
// Bottom shadow
UIBezierPath *fillPath = [UIBezierPath bezierPathWithRoundedRect:buttonFrame cornerRadius:radius];
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRect:frame];
[clipPath appendPath:fillPath];
[clipPath setUsesEvenOddFillRule:YES];
CGContextSaveGState(ctx);
[clipPath addClip];
[[UIColor blackColor] setFill];
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 1), 0, [UIColor colorWithWhite:1.0 alpha:0.25].CGColor);
[fillPath fill];
CGContextRestoreGState(ctx);
// Top shadow
CGContextSaveGState(ctx);
[fillPath addClip];
[[UIColor blackColor] setFill];
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 2), 0, [UIColor colorWithWhite:1.0 alpha:0.25].CGColor);
[clipPath fill];
CGContextRestoreGState(ctx);
// Button gradient
CGContextSaveGState(ctx);
[fillPath addClip];
CGContextDrawLinearGradient(ctx,
gradient,
CGPointMake(CGRectGetMidX(buttonFrame), CGRectGetMinY(buttonFrame)),
CGPointMake(CGRectGetMidX(buttonFrame), CGRectGetMaxY(buttonFrame)), 0);
CGContextRestoreGState(ctx);
// Draw highlight or down state
if (highlighted) {
CGContextSaveGState(ctx);
[[UIColor colorWithWhite:1.0 alpha:0.25] setFill];
[fillPath fill];
CGContextRestoreGState(ctx);
} else if (down) {
CGContextSaveGState(ctx);
[[UIColor colorWithWhite:0.0 alpha:0.25] setFill];
[fillPath fill];
CGContextRestoreGState(ctx);
}
// Button stroke
UIBezierPath *strokePath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(buttonFrame, strokeWidth / 2.0, strokeWidth / 2.0)
cornerRadius:radius - strokeWidth / 2.0];
[[UIColor colorWithWhite:0.0 alpha:0.8] setStroke];
[strokePath stroke];
// Draw title
CGFloat fontSize = 18.0;
CGRect textFrame = CGRectIntegral(CGRectMake(0, (CGRectGetHeight(rect) - fontSize) / 2.0 - 1.0, CGRectGetWidth(rect), fontSize));
CGContextSaveGState(ctx);
CGContextSetShadowWithColor(ctx, CGSizeMake(0.0, -1.0), 0.0, [UIColor blackColor].CGColor);
[[UIColor whiteColor] set];
[title drawInRect:textFrame withFont:self.titleFont lineBreakMode:UILineBreakModeMiddleTruncation alignment:UITextAlignmentCenter];
CGContextRestoreGState(ctx);
// Restore
CGContextRestoreGState(ctx);
}
- (void)drawTitleInRect:(CGRect)rect isSubtitle:(BOOL)isSubtitle {
NSString *title = (isSubtitle ? self.subtitle : self.title);
if (title.length > 0) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetShadowWithColor(ctx, CGSizeMake(0.0, -1.0), 0.0, [UIColor blackColor].CGColor);
UIFont *font = (isSubtitle ? self.subtitleFont : self.titleFont);
[[UIColor whiteColor] set];
[title drawInRect:rect withFont:font lineBreakMode:UILineBreakModeMiddleTruncation alignment:UITextAlignmentCenter];
CGContextRestoreGState(ctx);
}
}
- (void)drawSymbolInRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
// Color Declarations
UIColor *grey = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];
UIColor *black50 = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
// Gradient Declarations
NSArray *gradientColors = [NSArray arrayWithObjects:
(id)[UIColor whiteColor].CGColor,
(id)grey.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
// Shadow Declarations
CGColorRef shadow = black50.CGColor;
CGSize shadowOffset = CGSizeMake(0, 3);
CGFloat shadowBlurRadius = 3;
// Bezier Drawing
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
if (self.dialogStyle == CODialogStyleSuccess) {
[bezierPath moveToPoint:CGPointMake(16, 23)];
[bezierPath addLineToPoint:CGPointMake(27, 34)];
[bezierPath addLineToPoint:CGPointMake(56, 5)];
[bezierPath addLineToPoint:CGPointMake(63, 12)];
[bezierPath addLineToPoint:CGPointMake(27, 48)];
[bezierPath addLineToPoint:CGPointMake(9, 30)];
[bezierPath addLineToPoint:CGPointMake(16, 23)];
} else {
[bezierPath moveToPoint: CGPointMake(11, 17)];
[bezierPath addLineToPoint: CGPointMake(19, 9)];
[bezierPath addLineToPoint: CGPointMake(33, 23)];
[bezierPath addLineToPoint: CGPointMake(47, 9)];
[bezierPath addLineToPoint: CGPointMake(55, 17)];
[bezierPath addLineToPoint: CGPointMake(41, 31)];
[bezierPath addLineToPoint: CGPointMake(55, 45)];
[bezierPath addLineToPoint: CGPointMake(47, 53)];
[bezierPath addLineToPoint: CGPointMake(33, 39)];
[bezierPath addLineToPoint: CGPointMake(19, 53)];
[bezierPath addLineToPoint: CGPointMake(11, 45)];
[bezierPath addLineToPoint: CGPointMake(25, 31)];
[bezierPath addLineToPoint: CGPointMake(11, 17)];
}
[bezierPath closePath];
// Determine scale (the default side is 64)
CGPoint offset = CGPointMake((CGRectGetWidth(rect) - 64.0) / 2.0, (CGRectGetHeight(rect) - 64.0) / 2.0);
[bezierPath applyTransform:CGAffineTransformMakeTranslation(offset.x, offset.y)];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow);
CGContextSetFillColorWithColor(context, shadow);
[bezierPath fill];
[bezierPath addClip];
CGRect bounds = bezierPath.bounds;
CGContextDrawLinearGradient(context,
gradient,
CGPointMake(CGRectGetMidX(bounds), CGRectGetMinY(bounds)),
CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds)),
0);
CGContextRestoreGState(context);
// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
CGContextRestoreGState(ctx);
}
- (void)drawTextFieldInRect:(CGRect)rect {
// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// Color Declarations
UIColor *white10 = [UIColor colorWithWhite:1.0 alpha:0.1];
UIColor *grey40 = [UIColor colorWithWhite:0.4 alpha:1.0];
// Shadow Declarations
CGColorRef innerShadow = grey40.CGColor;
CGSize innerShadowOffset = CGSizeMake(0, 2);
CGFloat innerShadowBlurRadius = 2;
CGColorRef outerShadow = white10.CGColor;
CGSize outerShadowOffset = CGSizeMake(0, 1);
CGFloat outerShadowBlurRadius = 0;
// Rectangle Drawing
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRect: CGRectIntegral(rect)];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, outerShadowOffset, outerShadowBlurRadius, outerShadow);
[[UIColor whiteColor] setFill];
[rectanglePath fill];
// Rectangle Inner Shadow
CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -innerShadowBlurRadius, -innerShadowBlurRadius);
rectangleBorderRect = CGRectOffset(rectangleBorderRect, -innerShadowOffset.width, -innerShadowOffset.height);
rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1);
UIBezierPath* rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect];
[rectangleNegativePath appendPath: rectanglePath];
rectangleNegativePath.usesEvenOddFillRule = YES;
CGContextSaveGState(context);
{
CGFloat xOffset = innerShadowOffset.width + round(rectangleBorderRect.size.width);
CGFloat yOffset = innerShadowOffset.height;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
innerShadowBlurRadius,
innerShadow);
[rectanglePath addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0);
[rectangleNegativePath applyTransform: transform];
[[UIColor grayColor] setFill];
[rectangleNegativePath fill];
}
CGContextRestoreGState(context);
CGContextRestoreGState(context);
[[UIColor blackColor] setStroke];
rectanglePath.lineWidth = 1;
[rectanglePath stroke];
CGContextRestoreGState(context);
}
- (void)drawDimmedBackgroundInRect:(CGRect)rect {
// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
// Color Declarations
UIColor *greyInner = [UIColor colorWithWhite:0.0 alpha:0.70];
UIColor *greyOuter = [UIColor colorWithWhite:0.0 alpha:0.2];
// Gradient Declarations
NSArray* gradientColors = [NSArray arrayWithObjects:
(id)greyOuter.CGColor,
(id)greyInner.CGColor, nil];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
// Rectangle Drawing
CGPoint mid = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect:rect];
CGContextSaveGState(context);
[rectanglePath addClip];
CGContextDrawRadialGradient(context,
gradient,
mid, 10,
mid, CGRectGetMidY(rect),
kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(context);
// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
- (void)drawRect:(CGRect)rect {
[self drawDialogBackgroundInRect:rect];
[self drawTitleInRect:layout.titleRect isSubtitle:NO];
[self drawTitleInRect:layout.subtitleRect isSubtitle:YES];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Cylce through text fields
NSUInteger index = [self.textFields indexOfObject:textField];
NSUInteger count = self.textFields.count;
if (index < (count - 1)) {
UITextField *nextField = [self.textFields objectAtIndex:index + 1];
[nextField becomeFirstResponder];
} else {
[textField resignFirstResponder];
}
return YES;
}
@end
@implementation CODialogTextField
CODialogSynth(dialog)
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 4.0, 4.0);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
- (void)drawRect:(CGRect)rect {
[self.dialog drawTextFieldInRect:rect];
}
@end
@implementation CODialogWindowOverlay
CODialogSynth(dialog)
- (void)drawRect:(CGRect)rect {
[self.dialog drawDimmedBackgroundInRect:rect];
}
@end