-
Notifications
You must be signed in to change notification settings - Fork 944
/
MDCTextControlTextAreaContentViewController.m
237 lines (205 loc) · 9.16 KB
/
MDCTextControlTextAreaContentViewController.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
// Copyright 2020-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 "MDCTextControlTextAreaContentViewController.h"
#import "MDCTextControlContentViewController.h"
#import "MDCBaseTextArea.h"
#import "MaterialColorScheme.h"
#import "MaterialContainerScheme.h"
#import "MDCTextControlLabelBehavior.h"
#import "MDCFilledTextArea.h"
#import "MDCFilledTextArea+MaterialTheming.h"
#import "MDCOutlinedTextArea.h"
#import "MDCOutlinedTextArea+MaterialTheming.h"
@interface MDCTextControlTextAreaContentViewController () <UITextViewDelegate>
@property(nonatomic, assign) BOOL shouldAddDebugBorder;
@property(nonatomic, assign) BOOL shouldAddDebugLeadingView;
@end
@implementation MDCTextControlTextAreaContentViewController
#pragma mark Setup
- (MDCFilledTextArea *)createMaterialFilledTextArea {
MDCFilledTextArea *textArea = [[MDCFilledTextArea alloc] init];
textArea.textView.delegate = self;
textArea.labelBehavior = MDCTextControlLabelBehaviorFloats;
textArea.label.text = @"This is label text";
textArea.leadingAssistiveLabel.text = @"This is assistive label text.";
if (self.shouldAddDebugBorder) {
[self addBorderToTextArea:textArea];
}
if (self.shouldAddDebugLeadingView) {
[self addLeadingViewToTextArea:textArea];
}
[textArea applyThemeWithScheme:self.containerScheme];
return textArea;
}
- (MDCOutlinedTextArea *)createMaterialOutlinedTextArea {
MDCOutlinedTextArea *textArea = [[MDCOutlinedTextArea alloc] init];
textArea.textView.delegate = self;
textArea.label.text = @"This is label text";
textArea.leadingAssistiveLabel.text = @"This is assistive label text.";
if (self.shouldAddDebugBorder) {
[self addBorderToTextArea:textArea];
}
if (self.shouldAddDebugLeadingView) {
[self addLeadingViewToTextArea:textArea];
}
[textArea applyThemeWithScheme:self.containerScheme];
return textArea;
}
- (MDCBaseTextArea *)createDefaultBaseTextArea {
MDCBaseTextArea *textArea = [[MDCBaseTextArea alloc] init];
textArea.textView.delegate = self;
textArea.label.text = @"This is label text";
textArea.leadingAssistiveLabel.text = @"This is assistive label text.";
if (self.shouldAddDebugBorder) {
[self addBorderToTextArea:textArea];
}
if (self.shouldAddDebugLeadingView) {
[self addLeadingViewToTextArea:textArea];
}
return textArea;
}
- (void)textViewDidChange:(UITextView *)textView {
[self.view setNeedsLayout];
}
- (void)addBorderToTextArea:(MDCBaseTextArea *)textArea {
textArea.layer.borderColor = UIColor.redColor.CGColor;
textArea.layer.borderWidth = 1;
}
- (void)addLeadingViewToTextArea:(MDCBaseTextArea *)textArea {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
imageView.tintColor = self.containerScheme.colorScheme.primaryColor;
UIImage *image =
[[UIImage imageNamed:@"ic_cake"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.image = image;
textArea.leadingView = imageView;
textArea.leadingViewMode = UITextFieldViewModeAlways;
}
#pragma mark Overrides
- (void)initializeScrollViewSubviewsArray {
[super initializeScrollViewSubviewsArray];
self.shouldAddDebugBorder = NO;
self.shouldAddDebugLeadingView = NO;
MDCFilledTextArea *filledTextAreaWithoutFloatingLabel = [self createMaterialFilledTextArea];
filledTextAreaWithoutFloatingLabel.labelBehavior = MDCTextControlLabelBehaviorDisappears;
MDCOutlinedTextArea *outlinedTextAreaWithoutFloatingLabel = [self createMaterialOutlinedTextArea];
outlinedTextAreaWithoutFloatingLabel.labelBehavior = MDCTextControlLabelBehaviorDisappears;
MDCBaseTextArea *baseTextAreaWithoutFloatingLabel = [self createDefaultBaseTextArea];
baseTextAreaWithoutFloatingLabel.labelBehavior = MDCTextControlLabelBehaviorDisappears;
NSArray *textAreaRelatedScrollViewSubviews = @[
[self createLabelWithText:@"MDCFilledTextArea:"],
[self createMaterialFilledTextArea],
[self createLabelWithText:@"MDCFilledTextArea without floating label:"],
filledTextAreaWithoutFloatingLabel,
[self createLabelWithText:@"MDCOutlinedTextArea:"],
[self createMaterialOutlinedTextArea],
[self createLabelWithText:@"MDCOutlinedTextArea without floating label:"],
outlinedTextAreaWithoutFloatingLabel,
[self createLabelWithText:@"MDCBaseTextArea:"],
[self createDefaultBaseTextArea],
[self createLabelWithText:@"MDCBaseTextArea without floating label:"],
baseTextAreaWithoutFloatingLabel,
];
NSMutableArray *mutableScrollViewSubviews = [self.scrollViewSubviews mutableCopy];
self.scrollViewSubviews =
[mutableScrollViewSubviews arrayByAddingObjectsFromArray:textAreaRelatedScrollViewSubviews];
}
- (void)applyThemesToScrollViewSubviews {
[super applyThemesToScrollViewSubviews];
[self applyThemesToTextAreas];
}
- (void)resizeScrollViewSubviews {
[super resizeScrollViewSubviews];
[self resizeTextAreas];
}
- (void)enforcePreferredFonts {
[super enforcePreferredFonts];
[self.allTextAreas
enumerateObjectsUsingBlock:^(MDCBaseTextArea *textArea, NSUInteger idx, BOOL *stop) {
textArea.textView.adjustsFontForContentSizeCategory = YES;
textArea.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody
compatibleWithTraitCollection:textArea.traitCollection];
textArea.leadingAssistiveLabel.font =
[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2
compatibleWithTraitCollection:textArea.traitCollection];
textArea.trailingAssistiveLabel.font =
[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2
compatibleWithTraitCollection:textArea.traitCollection];
}];
}
- (void)handleResignFirstResponderTapped {
[super handleResignFirstResponderTapped];
[self.allTextAreas
enumerateObjectsUsingBlock:^(MDCBaseTextArea *textArea, NSUInteger idx, BOOL *stop) {
[textArea.textView resignFirstResponder];
}];
}
- (void)handleDisableButtonTapped {
[super handleDisableButtonTapped];
[self.allTextAreas enumerateObjectsUsingBlock:^(MDCBaseTextArea *_Nonnull textArea,
NSUInteger idx, BOOL *_Nonnull stop) {
textArea.enabled = !self.isDisabled;
}];
}
#pragma mark Private helper methods
- (void)resizeTextAreas {
CGFloat textAreaWidth = CGRectGetWidth(self.view.frame) - (2 * self.defaultPadding);
[self.allTextAreas
enumerateObjectsUsingBlock:^(MDCBaseTextArea *textArea, NSUInteger idx, BOOL *stop) {
CGFloat textAreaMinX = CGRectGetMinX(textArea.frame);
CGFloat textAreaMinY = CGRectGetMinY(textArea.frame);
CGFloat viewHeight = CGRectGetHeight(textArea.frame);
CGRect viewFrame = CGRectMake(textAreaMinX, textAreaMinY, textAreaWidth, viewHeight);
textArea.frame = viewFrame;
[textArea sizeToFit];
}];
}
- (void)applyThemesToTextAreas {
[self.allTextAreas
enumerateObjectsUsingBlock:^(MDCBaseTextArea *textArea, NSUInteger idx, BOOL *stop) {
BOOL isEven = idx % 2 == 0;
if (self.isErrored) {
if ([textArea isKindOfClass:[MDCFilledTextArea class]]) {
MDCFilledTextArea *filledTextArea = (MDCFilledTextArea *)textArea;
[filledTextArea applyErrorThemeWithScheme:self.containerScheme];
} else if ([textArea isKindOfClass:[MDCOutlinedTextArea class]]) {
MDCOutlinedTextArea *outlinedTextArea = (MDCOutlinedTextArea *)textArea;
[outlinedTextArea applyErrorThemeWithScheme:self.containerScheme];
}
if (isEven) {
textArea.leadingAssistiveLabel.text = @"Suspendisse quam elit, mattis sit amet justo "
@"vel, venenatis lobortis massa. Donec metus "
@"dolor.";
} else {
textArea.leadingAssistiveLabel.text = @"This is an error.";
}
} else {
if ([textArea isKindOfClass:[MDCFilledTextArea class]]) {
MDCFilledTextArea *filledTextArea = (MDCFilledTextArea *)textArea;
[filledTextArea applyThemeWithScheme:self.containerScheme];
} else if ([textArea isKindOfClass:[MDCOutlinedTextArea class]]) {
MDCOutlinedTextArea *outlinedTextArea = (MDCOutlinedTextArea *)textArea;
[outlinedTextArea applyThemeWithScheme:self.containerScheme];
}
if (isEven) {
textArea.leadingAssistiveLabel.text = @"This is helper text.";
} else {
textArea.leadingAssistiveLabel.text = nil;
}
}
}];
}
- (NSArray<MDCBaseTextArea *> *)allTextAreas {
return [self allScrollViewSubviewsOfClass:[MDCBaseTextArea class]];
}
@end