/
MDCTextInputControllerLegacyDefault.m
1361 lines (1129 loc) · 51.2 KB
/
MDCTextInputControllerLegacyDefault.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
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2016-present the Material Components for iOS authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "MDCTextInputControllerLegacyDefault.h"
#import "MDCMultilineTextField.h"
#import "MDCTextField.h"
#import "MDCTextInput.h"
#import "MDCTextInputCharacterCounter.h"
#import "MDCTextInputUnderlineView.h"
#import "private/MDCTextInputArt.h"
#import "MaterialAnimationTiming.h"
#import "MaterialMath.h"
#import "MaterialPalettes.h"
#import "MaterialRTL.h"
#import "MaterialTypography.h"
#pragma mark - Constants
static const CGFloat MDCTextInputControllerLegacyDefaultClearButtonImageSquareWidthHeight = 24.f;
static const CGFloat MDCTextInputControllerLegacyDefaultFloatingPlaceholderScaleDefault = 0.75f;
static const CGFloat MDCTextInputControllerLegacyDefaultHintTextOpacity = 0.54f;
static const CGFloat MDCTextInputControllerLegacyDefaultUnderlineActiveHeight = 2.f;
static const CGFloat MDCTextInputControllerLegacyDefaultUnderlineNormalHeight = 1.f;
static const CGFloat MDCTextInputControllerLegacyDefaultVerticalHalfPadding = 8.f;
static const CGFloat MDCTextInputControllerLegacyDefaultVerticalPadding = 16.f;
static const NSTimeInterval
MDCTextInputControllerLegacyDefaultFloatingPlaceholderDownAnimationDuration = 0.266666f;
static const NSTimeInterval
MDCTextInputControllerLegacyDefaultFloatingPlaceholderUpAnimationDuration = 0.3f;
static NSString *const MDCTextInputControllerLegacyDefaultActiveColorKey =
@"MDCTextInputControllerLegacyDefaultActiveColorKey";
static NSString *const MDCTextInputControllerLegacyDefaultCharacterCounterKey =
@"MDCTextInputControllerLegacyDefaultCharacterCounterKey";
static NSString *const MDCTextInputControllerLegacyDefaultCharacterCountViewModeKey =
@"MDCTextInputControllerLegacyDefaultCharacterCountViewModeKey";
static NSString *const MDCTextInputControllerLegacyDefaultCharacterCountMaxKey =
@"MDCTextInputControllerLegacyDefaultCharacterCountMaxKey";
static NSString *const MDCTextInputControllerLegacyDefaultDisabledColorKey =
@"MDCTextInputControllerLegacyDefaultDisabledColorKey";
static NSString *const MDCTextInputControllerLegacyDefaultErrorAccessibilityValueKey =
@"MDCTextInputControllerLegacyDefaultErrorAccessibilityValueKey";
static NSString *const MDCTextInputControllerLegacyDefaultErrorColorKey =
@"MDCTextInputControllerLegacyDefaultErrorColorKey";
static NSString *const MDCTextInputControllerLegacyDefaultErrorTextKey =
@"MDCTextInputControllerLegacyDefaultErrorTextKey";
static NSString *const MDCTextInputControllerLegacyDefaultFloatingEnabledKey =
@"MDCTextInputControllerLegacyDefaultFloatingEnabledKey";
static NSString *const MDCTextInputControllerLegacyDefaultFloatingPlaceholderNormalColorKey =
@"MDCTextInputControllerLegacyDefaultFloatingPlaceholderNormalColorKey";
static NSString *const MDCTextInputControllerLegacyDefaultFloatingPlaceholderScaleKey =
@"MDCTextInputControllerLegacyDefaultFloatingPlaceholderScaleKey";
static NSString *const MDCTextInputControllerLegacyDefaultHelperTextKey =
@"MDCTextInputControllerLegacyDefaultHelperTextKey";
static NSString *const MDCTextInputControllerLegacyDefaultInlinePlaceholderFontKey =
@"MDCTextInputControllerLegacyDefaultInlinePlaceholderFontKey";
static NSString *const MDCTextInputControllerLegacyDefaultInlinePlaceholderColorKey =
@"MDCTextInputControllerLegacyDefaultInlinePlaceholderColorKey";
static NSString *const MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelFontKey =
@"MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelFontKey";
static NSString *const MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelTextColor =
@"MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelTextColor";
static NSString *const MDCTextInputControllerLegacyDefaultNormalColorKey =
@"MDCTextInputControllerLegacyDefaultNormalColorKey";
static NSString *const MDCTextInputControllerLegacyDefaultPresentationStyleKey =
@"MDCTextInputControllerLegacyDefaultPresentationStyleKey";
static NSString *const MDCTextInputControllerLegacyDefaultTextInputKey =
@"MDCTextInputControllerLegacyDefaultTextInputKey";
static NSString *const MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelFontKey =
@"MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelFontKey";
static NSString *const MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelTextColor =
@"MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelTextColor";
static NSString *const MDCTextInputControllerLegacyDefaultUnderlineViewModeKey =
@"MDCTextInputControllerLegacyDefaultUnderlineViewModeKey";
static inline UIBezierPath *MDCTextInputControllerLegacyDefaultEmptyPath() {
return [UIBezierPath bezierPath];
}
static inline UIColor *MDCTextInputControllerLegacyDefaultInlinePlaceholderTextColorDefault() {
return [UIColor colorWithWhite:0 alpha:MDCTextInputControllerLegacyDefaultHintTextOpacity];
}
static inline UIColor *MDCTextInputControllerLegacyDefaultActiveColorDefault() {
return [MDCPalette bluePalette].accent700;
}
static inline UIColor *MDCTextInputControllerLegacyDefaultNormalUnderlineColorDefault() {
return [UIColor lightGrayColor];
}
static inline UIColor *MDCTextInputControllerLegacyDefaultTextErrorColorDefault() {
return [MDCPalette redPalette].accent400;
}
#pragma mark - Class Properties
static BOOL _floatingEnabledDefault = YES;
static BOOL _mdc_adjustsFontForContentSizeCategoryDefault = YES;
static CGFloat _floatingPlaceholderScaleDefault =
MDCTextInputControllerLegacyDefaultFloatingPlaceholderScaleDefault;
static UIColor *_activeColorDefault;
static UIColor *_disabledColorDefault;
static UIColor *_errorColorDefault;
static UIColor *_floatingPlaceholderNormalColorDefault;
static UIColor *_inlinePlaceholderColorDefault;
static UIColor *_leadingUnderlineLabelTextColorDefault;
static UIColor *_normalColorDefault;
static UIColor *_trailingUnderlineLabelTextColorDefault;
static UIFont *_inlinePlaceholderFontDefault;
static UIFont *_leadingUnderlineLabelFontDefault;
static UIFont *_trailingUnderlineLabelFontDefault;
static UITextFieldViewMode _underlineViewModeDefault = UITextFieldViewModeWhileEditing;
@interface MDCTextInputControllerLegacyDefault () {
BOOL _mdc_adjustsFontForContentSizeCategory;
MDCTextInputAllCharactersCounter *_characterCounter;
NSNumber *_floatingPlaceholderScale;
UIColor *_activeColor;
UIColor *_disabledColor;
UIColor *_errorColor;
UIColor *_floatingPlaceholderNormalColor;
UIColor *_inlinePlaceholderColor;
UIColor *_leadingUnderlineLabelTextColor;
UIColor *_normalColor;
UIColor *_trailingUnderlineLabelTextColor;
UIFont *_inlinePlaceholderFont;
UIFont *_leadingUnderlineLabelFont;
UIFont *_trailingUnderlineLabelFont;
}
@property(nonatomic, assign, readonly) BOOL isDisplayingCharacterCountError;
@property(nonatomic, assign, readonly) BOOL isDisplayingErrorText;
@property(nonatomic, assign, readonly) BOOL isPlaceholderUp;
@property(nonatomic, strong) MDCTextInputAllCharactersCounter *internalCharacterCounter;
@property(nonatomic, strong) NSLayoutConstraint *placeholderAnimationConstraintLeading;
@property(nonatomic, strong) NSLayoutConstraint *placeholderAnimationConstraintTop;
@property(nonatomic, strong) NSArray<NSLayoutConstraint *> *placeholderAnimationConstraints;
@property(nonatomic, copy) NSString *errorAccessibilityValue;
@property(nonatomic, copy, readwrite) NSString *errorText;
@property(nonatomic, copy) NSString *previousLeadingText;
@end
@implementation MDCTextInputControllerLegacyDefault
@synthesize characterCountMax = _characterCountMax;
@synthesize characterCountViewMode = _characterCountViewMode;
@synthesize textInput = _textInput;
@synthesize underlineViewMode = _underlineViewMode;
// TODO: (larche): Support in-line auto complete.
- (instancetype)init {
self = [super init];
if (self) {
[self commonMDCTextInputControllerLegacyDefaultInitialization];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
[self commonMDCTextInputControllerLegacyDefaultInitialization];
_activeColor = [aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultActiveColorKey];
_characterCounter =
[aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultCharacterCounterKey];
_characterCountMax =
[aDecoder decodeIntegerForKey:MDCTextInputControllerLegacyDefaultCharacterCountMaxKey];
_characterCountViewMode =
[aDecoder decodeIntegerForKey:MDCTextInputControllerLegacyDefaultCharacterCountViewModeKey];
_disabledColor =
[aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultDisabledColorKey];
_errorColor = [aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultErrorColorKey];
_floatingEnabled =
[aDecoder decodeBoolForKey:MDCTextInputControllerLegacyDefaultFloatingEnabledKey];
_floatingPlaceholderNormalColor = [aDecoder
decodeObjectForKey:MDCTextInputControllerLegacyDefaultFloatingPlaceholderNormalColorKey];
_floatingPlaceholderScale = [aDecoder
decodeObjectForKey:MDCTextInputControllerLegacyDefaultFloatingPlaceholderScaleKey];
_inlinePlaceholderColor =
[aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultInlinePlaceholderColorKey];
_leadingUnderlineLabelTextColor = [aDecoder
decodeObjectForKey:MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelTextColor];
_normalColor = [aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultNormalColorKey];
_textInput = [aDecoder decodeObjectForKey:MDCTextInputControllerLegacyDefaultTextInputKey];
_trailingUnderlineLabelTextColor = [aDecoder
decodeObjectForKey:MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelTextColor];
_underlineViewMode = (UITextFieldViewMode)
[aDecoder decodeIntegerForKey:MDCTextInputControllerLegacyDefaultUnderlineViewModeKey];
}
return self;
}
- (instancetype)initWithTextInput:(UIView<MDCTextInput> *)textInput {
self = [self init];
if (self) {
_textInput = textInput;
// This should happen last because it relies on the state of a ton of properties.
[self setupInput];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.activeColor forKey:MDCTextInputControllerLegacyDefaultActiveColorKey];
if ([self.characterCounter conformsToProtocol:@protocol(NSCoding)]) {
[aCoder encodeObject:self.characterCounter
forKey:MDCTextInputControllerLegacyDefaultCharacterCounterKey];
}
[aCoder encodeInteger:self.characterCountMax
forKey:MDCTextInputControllerLegacyDefaultCharacterCountMaxKey];
[aCoder encodeInteger:self.characterCountViewMode
forKey:MDCTextInputControllerLegacyDefaultCharacterCountViewModeKey];
[aCoder encodeObject:self.disabledColor
forKey:MDCTextInputControllerLegacyDefaultDisabledColorKey];
[aCoder encodeObject:self.errorAccessibilityValue
forKey:MDCTextInputControllerLegacyDefaultErrorAccessibilityValueKey];
[aCoder encodeObject:self.errorColor forKey:MDCTextInputControllerLegacyDefaultErrorColorKey];
[aCoder encodeObject:self.errorText forKey:MDCTextInputControllerLegacyDefaultErrorTextKey];
[aCoder encodeBool:self.isFloatingEnabled
forKey:MDCTextInputControllerLegacyDefaultFloatingEnabledKey];
[aCoder encodeObject:self.floatingPlaceholderNormalColor
forKey:MDCTextInputControllerLegacyDefaultFloatingPlaceholderNormalColorKey];
[aCoder encodeObject:self.floatingPlaceholderScale
forKey:MDCTextInputControllerLegacyDefaultFloatingPlaceholderScaleKey];
[aCoder encodeObject:self.helperText forKey:MDCTextInputControllerLegacyDefaultHelperTextKey];
[aCoder encodeObject:self.inlinePlaceholderColor
forKey:MDCTextInputControllerLegacyDefaultInlinePlaceholderColorKey];
[aCoder encodeObject:self.inlinePlaceholderFont
forKey:MDCTextInputControllerLegacyDefaultInlinePlaceholderFontKey];
[aCoder encodeObject:self.leadingUnderlineLabelFont
forKey:MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelFontKey];
[aCoder encodeObject:self.leadingUnderlineLabelTextColor
forKey:MDCTextInputControllerLegacyDefaultLeadingUnderlineLabelTextColor];
[aCoder encodeObject:self.normalColor forKey:MDCTextInputControllerLegacyDefaultNormalColorKey];
[aCoder encodeConditionalObject:self.textInput
forKey:MDCTextInputControllerLegacyDefaultTextInputKey];
[aCoder encodeObject:self.trailingUnderlineLabelFont
forKey:MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelFontKey];
[aCoder encodeObject:self.trailingUnderlineLabelTextColor
forKey:MDCTextInputControllerLegacyDefaultTrailingUnderlineLabelTextColor];
[aCoder encodeInteger:self.underlineViewMode
forKey:MDCTextInputControllerLegacyDefaultUnderlineViewModeKey];
}
- (instancetype)copyWithZone:(__unused NSZone *)zone {
MDCTextInputControllerLegacyDefault *copy = [[[self class] alloc] init];
copy.activeColor = self.activeColor;
copy.characterCounter = self.characterCounter; // Just a pointer value copy
copy.characterCountViewMode = self.characterCountViewMode;
copy.characterCountMax = self.characterCountMax;
copy.disabledColor = self.disabledColor;
copy.errorAccessibilityValue = [self.errorAccessibilityValue copy];
copy.errorColor = self.errorColor;
copy.errorText = [self.errorText copy];
copy.floatingEnabled = self.isFloatingEnabled;
copy.floatingPlaceholderNormalColor = self.floatingPlaceholderNormalColor;
copy.floatingPlaceholderScale = self.floatingPlaceholderScale;
copy.helperText = [self.helperText copy];
copy.inlinePlaceholderColor = self.inlinePlaceholderColor;
copy.inlinePlaceholderFont = self.inlinePlaceholderFont;
copy.leadingUnderlineLabelFont = self.leadingUnderlineLabelFont;
copy.leadingUnderlineLabelTextColor = self.leadingUnderlineLabelTextColor;
copy.normalColor = self.normalColor;
copy.previousLeadingText = [self.previousLeadingText copy];
copy.textInput = self.textInput; // Just a pointer value copy
copy.trailingUnderlineLabelFont = self.trailingUnderlineLabelFont;
copy.trailingUnderlineLabelTextColor = self.trailingUnderlineLabelTextColor;
copy.underlineViewMode = self.underlineViewMode;
return copy;
}
- (void)dealloc {
[self unsubscribeFromNotifications];
}
- (void)commonMDCTextInputControllerLegacyDefaultInitialization {
_characterCountViewMode = UITextFieldViewModeAlways;
_disabledColor = [self class].disabledColorDefault;
_floatingEnabled = [self class].isFloatingEnabledDefault;
_internalCharacterCounter = [[MDCTextInputAllCharactersCounter alloc] init];
_underlineViewMode = [self class].underlineViewModeDefault;
_textInput.hidesPlaceholderOnInput = NO;
[self forceUpdatePlaceholderY];
}
- (void)setupInput {
if (!_textInput) {
return;
}
// This controller will handle Dynamic Type and all fonts for the text input
_mdc_adjustsFontForContentSizeCategory =
_textInput.mdc_adjustsFontForContentSizeCategory ||
[self class].mdc_adjustsFontForContentSizeCategoryDefault;
_textInput.underline.disabledColor = self.disabledColor;
_textInput.mdc_adjustsFontForContentSizeCategory = NO;
_textInput.positioningDelegate = self;
_textInput.hidesPlaceholderOnInput = !self.isFloatingEnabled;
[self setupClearButton];
[self subscribeForNotifications];
_textInput.underline.color = [self class].normalColorDefault;
[self forceUpdatePlaceholderY];
}
- (void)setupClearButton {
UIImage *image = [self
drawnClearButtonImage:[UIColor colorWithWhite:0 alpha:[MDCTypography captionFontOpacity]]];
[_textInput.clearButton setImage:image forState:UIControlStateNormal];
[_textInput.clearButton setImage:image forState:UIControlStateNormal];
[_textInput.clearButton setImage:image forState:UIControlStateNormal];
}
- (void)subscribeForNotifications {
if (!_textInput) {
return;
}
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
if ([_textInput isKindOfClass:[UITextField class]]) {
[defaultCenter addObserver:self
selector:@selector(textInputDidBeginEditing:)
name:UITextFieldTextDidBeginEditingNotification
object:_textInput];
[defaultCenter addObserver:self
selector:@selector(textInputDidChange:)
name:UITextFieldTextDidChangeNotification
object:_textInput];
[defaultCenter addObserver:self
selector:@selector(textInputDidEndEditing:)
name:UITextFieldTextDidEndEditingNotification
object:_textInput];
[defaultCenter addObserver:self
selector:@selector(textInputDidChange:)
name:MDCTextFieldTextDidSetTextNotification
object:_textInput];
}
if ([_textInput isKindOfClass:[MDCMultilineTextField class]]) {
MDCMultilineTextField *textField = (MDCMultilineTextField *)_textInput;
[defaultCenter addObserver:self
selector:@selector(textInputDidBeginEditing:)
name:UITextViewTextDidBeginEditingNotification
object:textField.textView];
[defaultCenter addObserver:self
selector:@selector(textInputDidChange:)
name:UITextViewTextDidChangeNotification
object:textField.textView];
[defaultCenter addObserver:self
selector:@selector(textInputDidEndEditing:)
name:UITextViewTextDidEndEditingNotification
object:textField.textView];
}
}
- (void)unsubscribeFromNotifications {
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver:self];
}
#pragma mark - Border Customization
- (void)updateBorder {
self.textInput.borderPath = MDCTextInputControllerLegacyDefaultEmptyPath();
}
#pragma mark - Character Max Implementation
- (NSUInteger)characterCount {
return [self.characterCounter characterCountForTextInput:self.textInput];
}
- (id<MDCTextInputCharacterCounter>)characterCounter {
if (!_characterCounter) {
_characterCounter = self.internalCharacterCounter;
}
return _characterCounter;
}
- (void)setCharacterCounter:(id<MDCTextInputCharacterCounter>)characterCounter {
if (_characterCounter != characterCounter) {
_characterCounter = characterCounter;
[self updateLayout];
}
}
- (void)setCharacterCountMax:(NSUInteger)characterCountMax {
if (_characterCountMax != characterCountMax) {
_characterCountMax = characterCountMax;
[self updateLayout];
}
}
#pragma mark - Clear Button Customization
- (UIImage *)drawnClearButtonImage:(UIColor *)color {
CGSize clearButtonSize =
CGSizeMake(MDCTextInputControllerLegacyDefaultClearButtonImageSquareWidthHeight,
MDCTextInputControllerLegacyDefaultClearButtonImageSquareWidthHeight);
CGFloat scale = [UIScreen mainScreen].scale;
CGRect bounds = CGRectMake(0, 0, clearButtonSize.width * scale, clearButtonSize.height * scale);
UIGraphicsBeginImageContextWithOptions(bounds.size, false, scale);
[color setFill];
[MDCPathForClearButtonLegacyImageFrame(bounds) fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
return image;
}
#pragma mark - Leading Label Customization
- (void)updateLeadingUnderlineLabel {
self.textInput.leadingUnderlineLabel.font = self.leadingUnderlineLabelFont;
self.textInput.leadingUnderlineLabel.textColor =
(self.isDisplayingErrorText || self.isDisplayingCharacterCountError)
? self.errorColor
: self.leadingUnderlineLabelTextColor;
}
#pragma mark - Placeholder Customization
- (void)updatePlaceholder {
self.textInput.placeholderLabel.font = self.inlinePlaceholderFont;
if (self.isPlaceholderUp) {
UIColor *nonErrorColor = self.textInput.isEditing ? self.activeColor :
self.floatingPlaceholderNormalColor;
self.textInput.placeholderLabel.textColor =
(self.isDisplayingCharacterCountError || self.isDisplayingErrorText)
? self.errorColor : nonErrorColor;
} else {
self.textInput.placeholderLabel.textColor = self.inlinePlaceholderColor;
}
}
// Sometimes the text field is not showing the correct layout for its values (like when it's created
// with .text already entered) so we make sure it's in the right place always.
//
// Note that this calls updateLayout inside it so it is the only 'update-' method not included in
// updateLayout.
- (void)forceUpdatePlaceholderY {
BOOL isDirectionToUp = NO;
if (self.floatingEnabled) {
isDirectionToUp = self.textInput.text.length >= 1 || self.textInput.isEditing;
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
[self movePlaceholderToUp:isDirectionToUp];
[CATransaction commit];
[self updateLayout];
self.textInput.hidesPlaceholderOnInput = !self.floatingEnabled;
[self.textInput layoutIfNeeded];
}
- (BOOL)isPlaceholderUp {
return self.placeholderAnimationConstraints.count > 0 &&
!CGAffineTransformEqualToTransform(self.textInput.placeholderLabel.transform,
CGAffineTransformIdentity);
}
#pragma mark - Placeholder Animation
- (void)movePlaceholderToUp:(BOOL)isToUp {
if (self.isPlaceholderUp == isToUp) {
return;
}
CGFloat scaleFactor = (CGFloat)self.floatingPlaceholderScale.floatValue;
CGAffineTransform floatingPlaceholderScaleTransform =
CGAffineTransformMakeScale(scaleFactor, scaleFactor);
// The animation is accomplished pretty simply. A constraint for vertical and a constraint for
// horizontal offset, both with a required priority (1000), are acivated on the placeholderLabel.
// A simple scale transform is also applied. Then it's animated through the UIView animation API
// (layoutIfNeeded). If in reverse (isToUp == NO), these things are just removed / deactivated.
CGAffineTransform destinationTransform = isToUp ? floatingPlaceholderScaleTransform : CGAffineTransformIdentity;
// We do this beforehand to flush the layout engine.
[self.textInput layoutIfNeeded];
[UIView animateWithDuration:[CATransaction animationDuration]
animations:^{
self.textInput.placeholderLabel.transform = destinationTransform;
[self updatePlaceholder];
[self updatePlaceholderAnimationConstraints:isToUp];
[self.textInput layoutIfNeeded];
}
completion:^(__unused BOOL finished) {
if (!isToUp) {
[self cleanupPlaceholderAnimationConstraints];
}
}];
}
- (void)updatePlaceholderAnimationConstraints:(BOOL)isToUp {
if (isToUp) {
CGPoint destination = [self placeholderFloatingPosition];
if (!self.placeholderAnimationConstraintLeading) {
self.placeholderAnimationConstraintLeading =
[NSLayoutConstraint constraintWithItem:self.textInput.placeholderLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.textInput
attribute:NSLayoutAttributeLeading
multiplier:1
constant:destination.x];
}
self.placeholderAnimationConstraintLeading.constant = destination.x;
if (!self.placeholderAnimationConstraintTop) {
self.placeholderAnimationConstraintTop =
[NSLayoutConstraint constraintWithItem:self.textInput.placeholderLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.textInput
attribute:NSLayoutAttributeTop
multiplier:1
constant:destination.y];
}
self.placeholderAnimationConstraintTop.constant = destination.y;
self.placeholderAnimationConstraints = @[ self.placeholderAnimationConstraintLeading,
self.placeholderAnimationConstraintTop ];
[NSLayoutConstraint activateConstraints:self.placeholderAnimationConstraints];
} else {
[NSLayoutConstraint deactivateConstraints:self.placeholderAnimationConstraints];
}
}
- (void)cleanupPlaceholderAnimationConstraints {
self.placeholderAnimationConstraints = nil;
self.placeholderAnimationConstraintLeading = nil;
self.placeholderAnimationConstraintTop = nil;
}
- (CGPoint)placeholderFloatingPosition {
CGFloat placeholderY = MDCTextInputControllerLegacyDefaultVerticalPadding;
// Offsets needed due to transform working on normal (0.5,0.5) anchor point.
// Why no anchor point of (0,0)? Because our users wouldn't expect it.
placeholderY -= self.textInput.placeholderLabel.font.lineHeight *
(1 - (CGFloat)self.floatingPlaceholderScale.floatValue) * .5f;
CGFloat estimatedWidth = MDCCeil(CGRectGetWidth([self.textInput.placeholderLabel.text
boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.textInput.placeholderLabel.font.lineHeight)
options:0
attributes:@{
NSFontAttributeName : self.textInput.font
}
context:nil]));
CGFloat placeholderX =
-1 * estimatedWidth * (1 - (CGFloat)self.floatingPlaceholderScale.floatValue) * .5f;
return CGPointMake(placeholderX, placeholderY);
}
#pragma mark - Trailing Label Customization
- (void)updateTrailingUnderlineLabel {
if (!self.characterCountMax) {
self.textInput.trailingUnderlineLabel.text = nil;
} else {
self.textInput.trailingUnderlineLabel.text = [self characterCountText];
self.textInput.trailingUnderlineLabel.font = self.trailingUnderlineLabelFont;
}
UIColor *textColor = self.trailingUnderlineLabelTextColor;
if (self.isDisplayingCharacterCountError || self.isDisplayingErrorText) {
textColor = self.errorColor;
}
switch (self.characterCountViewMode) {
case UITextFieldViewModeAlways:
break;
case UITextFieldViewModeWhileEditing:
textColor = !self.textInput.isEditing ? [UIColor clearColor] : textColor;
break;
case UITextFieldViewModeUnlessEditing:
textColor = self.textInput.isEditing ? [UIColor clearColor] : textColor;
break;
case UITextFieldViewModeNever:
textColor = [UIColor clearColor];
break;
}
self.textInput.trailingUnderlineLabel.textColor = textColor;
}
- (NSString *)characterCountText {
// TODO: (larche) Localize
return [NSString stringWithFormat:@"%lu / %lu", (unsigned long)[self characterCount],
(unsigned long)self.characterCountMax];
}
#pragma mark - Underline Customization
- (void)updateUnderline {
UIColor *underlineColor;
CGFloat underlineHeight;
switch (self.underlineViewMode) {
case UITextFieldViewModeAlways:
underlineColor = self.activeColor;
underlineHeight = MDCTextInputControllerLegacyDefaultUnderlineActiveHeight;
break;
case UITextFieldViewModeWhileEditing:
underlineColor = self.textInput.isEditing ? self.activeColor : self.normalColor;
underlineHeight = self.textInput.isEditing
? MDCTextInputControllerLegacyDefaultUnderlineActiveHeight
: MDCTextInputControllerLegacyDefaultUnderlineNormalHeight;
break;
case UITextFieldViewModeUnlessEditing:
underlineColor = !self.textInput.isEditing ? self.activeColor : self.normalColor;
underlineHeight = !self.textInput.isEditing
? MDCTextInputControllerLegacyDefaultUnderlineActiveHeight
: MDCTextInputControllerLegacyDefaultUnderlineNormalHeight;
break;
case UITextFieldViewModeNever:
default:
underlineColor = self.normalColor;
underlineHeight = MDCTextInputControllerLegacyDefaultUnderlineNormalHeight;
break;
}
self.textInput.underline.color =
(self.isDisplayingCharacterCountError || self.isDisplayingErrorText) ? self.errorColor
: underlineColor;
self.textInput.underline.lineHeight = underlineHeight;
self.textInput.underline.disabledColor = self.disabledColor;
}
#pragma mark - Underline Labels Fonts
+ (UIFont *)placeholderFont {
return [UIFont mdc_preferredFontForMaterialTextStyle:MDCFontTextStyleBody1];
}
+ (UIFont *)underlineLabelsFont {
return [UIFont mdc_preferredFontForMaterialTextStyle:MDCFontTextStyleCaption];
}
#pragma mark - Properties Implementation
- (UIColor *)activeColor {
return _activeColor ? _activeColor : [self class].activeColorDefault;
}
- (void)setActiveColor:(UIColor *)activeColor {
if (![_activeColor isEqual:activeColor]) {
_activeColor = activeColor;
[self updateUnderline];
}
}
+ (UIColor *)activeColorDefault {
if (!_activeColorDefault) {
_activeColorDefault = MDCTextInputControllerLegacyDefaultActiveColorDefault();
}
return _activeColorDefault;
}
+ (void)setActiveColorDefault:(UIColor *)activeColorDefault {
_activeColorDefault = activeColorDefault
? activeColorDefault
: MDCTextInputControllerLegacyDefaultActiveColorDefault();
}
- (void)setCharacterCountViewMode:(UITextFieldViewMode)characterCountViewMode {
if (_characterCountViewMode != characterCountViewMode) {
_characterCountViewMode = characterCountViewMode;
[self updateLayout];
}
}
- (UIColor *)disabledColor {
if (!_disabledColor) {
_disabledColor = [self class].disabledColorDefault;
}
return _disabledColor;
}
- (void)setDisabledColor:(UIColor *)disabledColor {
if (![_disabledColor isEqual:disabledColor]) {
_disabledColor = disabledColor ? disabledColor : [self class].disabledColorDefault;
self.textInput.underline.disabledColor = disabledColor;
}
}
+ (UIColor *)disabledColorDefault {
if (!_disabledColorDefault) {
_disabledColorDefault = MDCTextInputControllerLegacyDefaultNormalUnderlineColorDefault();
}
return _disabledColorDefault;
}
+ (void)setDisabledColorDefault:(UIColor *)disabledColorDefault {
_disabledColorDefault = disabledColorDefault
? disabledColorDefault
: MDCTextInputControllerLegacyDefaultNormalUnderlineColorDefault();
}
- (BOOL)isDisplayingCharacterCountError {
return self.characterCountMax && [self characterCount] > self.characterCountMax;
}
- (BOOL)isDisplayingErrorText {
return self.errorText != nil;
}
- (void)setErrorAccessibilityValue:(NSString *)errorAccessibilityValue {
_errorAccessibilityValue = [errorAccessibilityValue copy];
}
- (UIColor *)errorColor {
if (!_errorColor) {
_errorColor = [self class].errorColorDefault;
}
return _errorColor;
}
- (void)setErrorColor:(UIColor *)errorColor {
if (![_errorColor isEqual:errorColor]) {
_errorColor = errorColor ? errorColor : [self class].errorColorDefault;
if (self.isDisplayingCharacterCountError || self.isDisplayingErrorText) {
[self updateLeadingUnderlineLabel];
[self updatePlaceholder];
[self updateTrailingUnderlineLabel];
[self updateUnderline];
}
}
}
+ (UIColor *)errorColorDefault {
if (!_errorColorDefault) {
_errorColorDefault = MDCTextInputControllerLegacyDefaultTextErrorColorDefault();
}
return _errorColorDefault;
}
+ (void)setErrorColorDefault:(UIColor *)errorColorDefault {
_errorColorDefault = errorColorDefault
? errorColorDefault
: MDCTextInputControllerLegacyDefaultTextErrorColorDefault();
}
- (void)setErrorText:(NSString *)errorText {
_errorText = [errorText copy];
}
- (void)setFloatingPlaceholderNormalColor:(UIColor *)floatingPlaceholderNormalColor {
if (![_floatingPlaceholderNormalColor isEqual:floatingPlaceholderNormalColor]) {
_floatingPlaceholderNormalColor = floatingPlaceholderNormalColor;
[self updatePlaceholder];
}
}
- (UIColor *)floatingPlaceholderNormalColor {
return _floatingPlaceholderNormalColor ? _floatingPlaceholderNormalColor
: [self class].floatingPlaceholderNormalColorDefault;
}
+ (UIColor *)floatingPlaceholderNormalColorDefault {
if (!_floatingPlaceholderNormalColorDefault) {
_floatingPlaceholderNormalColorDefault = [self class].inlinePlaceholderColorDefault;
}
return _floatingPlaceholderNormalColorDefault;
}
+ (void)setFloatingPlaceholderNormalColorDefault:(UIColor *)floatingPlaceholderNormalColorDefault {
_floatingPlaceholderNormalColorDefault = floatingPlaceholderNormalColorDefault
? floatingPlaceholderNormalColorDefault
: [self class].inlinePlaceholderColorDefault;
}
- (void)setFloatingEnabled:(BOOL)floatingEnabled {
if (_floatingEnabled != floatingEnabled) {
_floatingEnabled = floatingEnabled;
[self forceUpdatePlaceholderY];
}
}
+ (BOOL)isFloatingEnabledDefault {
return _floatingEnabledDefault;
}
+ (void)setFloatingEnabledDefault:(BOOL)floatingEnabledDefault {
_floatingEnabledDefault = floatingEnabledDefault;
}
- (NSNumber *)floatingPlaceholderScale {
if (!_floatingPlaceholderScale) {
_floatingPlaceholderScale =
[NSNumber numberWithFloat:(float)[self class].floatingPlaceholderScaleDefault];
}
return _floatingPlaceholderScale;
}
- (void)setFloatingPlaceholderScale:(NSNumber *)floatingPlaceholderScale {
if (![_floatingPlaceholderScale isEqualToNumber:floatingPlaceholderScale]) {
_floatingPlaceholderScale =
floatingPlaceholderScale
? floatingPlaceholderScale
: [NSNumber numberWithFloat:(float)[self class].floatingPlaceholderScaleDefault];
[self updatePlaceholder];
}
}
+ (CGFloat)floatingPlaceholderScaleDefault {
return _floatingPlaceholderScaleDefault;
}
+ (void)setFloatingPlaceholderScaleDefault:(CGFloat)floatingPlaceholderScaleDefault {
_floatingPlaceholderScaleDefault = floatingPlaceholderScaleDefault;
}
- (void)setHelperText:(NSString *)helperText {
if (self.isDisplayingErrorText) {
self.previousLeadingText = helperText;
} else {
if (![self.textInput.leadingUnderlineLabel.text isEqualToString:helperText]) {
self.textInput.leadingUnderlineLabel.text = helperText;
[self updateLayout];
}
}
}
- (NSString *)helperText {
if (self.isDisplayingErrorText) {
return self.previousLeadingText;
} else {
return self.textInput.leadingUnderlineLabel.text;
}
}
- (void)setInlinePlaceholderColor:(UIColor *)inlinePlaceholderColor {
if (![_inlinePlaceholderColor isEqual:inlinePlaceholderColor]) {
_inlinePlaceholderColor = inlinePlaceholderColor;
[self updatePlaceholder];
}
}
- (UIColor *)inlinePlaceholderColor {
return _inlinePlaceholderColor ? _inlinePlaceholderColor
: [self class].inlinePlaceholderColorDefault;
}
+ (UIColor *)inlinePlaceholderColorDefault {
if (!_inlinePlaceholderColorDefault) {
_inlinePlaceholderColorDefault =
MDCTextInputControllerLegacyDefaultInlinePlaceholderTextColorDefault();
}
return _inlinePlaceholderColorDefault;
}
+ (void)setInlinePlaceholderColorDefault:(UIColor *)inlinePlaceholderColorDefault {
_inlinePlaceholderColorDefault =
inlinePlaceholderColorDefault
? inlinePlaceholderColorDefault
: MDCTextInputControllerLegacyDefaultInlinePlaceholderTextColorDefault();
}
- (UIFont *)inlinePlaceholderFont {
return _inlinePlaceholderFont ?: [self class].inlinePlaceholderFontDefault;
}
- (void)setInlinePlaceholderFont:(UIFont *)inlinePlaceholderFont {
if (![_inlinePlaceholderFont isEqual:inlinePlaceholderFont]) {
_inlinePlaceholderFont = inlinePlaceholderFont;
[self updateLayout];
}
}
+ (UIFont *)inlinePlaceholderFontDefault {
return _inlinePlaceholderFontDefault ?: [[self class] placeholderFont];
}
+ (void)setInlinePlaceholderFontDefault:(UIFont *)inlinePlaceholderFontDefault {
_inlinePlaceholderFontDefault = inlinePlaceholderFontDefault;
}
- (UIFont *)leadingUnderlineLabelFont {
return _leadingUnderlineLabelFont ?: [self class].leadingUnderlineLabelFontDefault;
}
- (void)setLeadingUnderlineLabelFont:(UIFont *)leadingUnderlineLabelFont {
if (![_leadingUnderlineLabelFont isEqual:leadingUnderlineLabelFont]) {
_leadingUnderlineLabelFont = leadingUnderlineLabelFont;
[self updateLayout];
}
}
+ (UIFont *)leadingUnderlineLabelFontDefault {
return _leadingUnderlineLabelFontDefault ?: [[self class] underlineLabelsFont];
}
+ (void)setLeadingUnderlineLabelFontDefault:(UIFont *)leadingUnderlineLabelFontDefault {
_leadingUnderlineLabelFontDefault = leadingUnderlineLabelFontDefault;
}
- (UIColor *)leadingUnderlineLabelTextColor {
return _leadingUnderlineLabelTextColor ? _leadingUnderlineLabelTextColor
: [self class].leadingUnderlineLabelTextColorDefault;
}
- (void)setLeadingUnderlineLabelTextColor:(UIColor *)leadingUnderlineLabelTextColor {
if (_leadingUnderlineLabelTextColor != leadingUnderlineLabelTextColor) {
_leadingUnderlineLabelTextColor = leadingUnderlineLabelTextColor
? leadingUnderlineLabelTextColor
: [self class].leadingUnderlineLabelTextColorDefault;
[self updateLeadingUnderlineLabel];
}
}
+ (UIColor *)leadingUnderlineLabelTextColorDefault {
if (!_leadingUnderlineLabelTextColorDefault) {
_leadingUnderlineLabelTextColorDefault =
MDCTextInputControllerLegacyDefaultInlinePlaceholderTextColorDefault();
}
return _leadingUnderlineLabelTextColorDefault;
}
+ (void)setLeadingUnderlineLabelTextColorDefault:(UIColor *)leadingUnderlineLabelTextColorDefault {
_leadingUnderlineLabelTextColorDefault =
leadingUnderlineLabelTextColorDefault
? leadingUnderlineLabelTextColorDefault
: MDCTextInputControllerLegacyDefaultInlinePlaceholderTextColorDefault();
}
- (UIColor *)normalColor {
return _normalColor ? _normalColor : [self class].normalColorDefault;
}
- (void)setNormalColor:(UIColor *)normalColor {
if (![_normalColor isEqual:normalColor]) {
_normalColor = normalColor;
[self updateUnderline];
}
}
+ (UIColor *)normalColorDefault {
if (!_normalColorDefault) {
_normalColorDefault = MDCTextInputControllerLegacyDefaultNormalUnderlineColorDefault();
}
return _normalColorDefault;
}
+ (void)setNormalColorDefault:(UIColor *)normalColorDefault {
_normalColorDefault = normalColorDefault