forked from material-components/material-components-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionViewListCell.m
358 lines (303 loc) · 14.6 KB
/
CollectionViewListCell.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
// Copyright 2018-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 "CollectionViewListCell.h"
#import <MDFInternationalization/MDFInternationalization.h>
#import "MaterialInk.h"
#import "MaterialTypography.h"
static const CGFloat kImagePadding = 16;
static const CGFloat kImageHeight = 40;
static const CGFloat kWithImageRightPadding = 12;
static const CGFloat kTextVerticalPadding = 12;
static const CGFloat kTitleColorOpacity = (CGFloat)0.87;
static const CGFloat kDetailsColorOpacity = (CGFloat)0.6;
static inline UIFont *defaultTitleFont(void) {
return [UIFont systemFontOfSize:16];
}
static inline UIFont *defaultDetailsFont(void) {
return [UIFont systemFontOfSize:14];
}
@implementation CollectionViewListCell {
CGPoint _lastTouch;
UIView *_contentWrapper;
NSLayoutConstraint *_cellWidthConstraint;
NSLayoutConstraint *_imageLeftPaddingConstraint;
NSLayoutConstraint *_imageRightPaddingConstraint;
NSLayoutConstraint *_imageWidthConstraint;
BOOL _mdc_adjustsFontForContentSizeCategory;
MDCInkView *_inkView;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonCollectionViewListCellInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonCollectionViewListCellInit];
}
return self;
}
- (void)commonCollectionViewListCellInit {
_inkView = [[MDCInkView alloc] initWithFrame:self.bounds];
_inkView.usesLegacyInkRipple = NO;
[self addSubview:_inkView];
_contentWrapper = [[UIView alloc] initWithFrame:self.contentView.bounds];
_contentWrapper.autoresizingMask =
UIViewAutoresizingFlexibleWidth | MDFTrailingMarginAutoresizingMaskForLayoutDirection(
self.mdf_effectiveUserInterfaceLayoutDirection);
_contentWrapper.clipsToBounds = YES;
[self.contentView addSubview:_contentWrapper];
// Text label.
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLabel.autoresizingMask = MDFTrailingMarginAutoresizingMaskForLayoutDirection(
self.mdf_effectiveUserInterfaceLayoutDirection);
// Detail text label.
_detailsTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_detailsTextLabel.autoresizingMask = MDFTrailingMarginAutoresizingMaskForLayoutDirection(
self.mdf_effectiveUserInterfaceLayoutDirection);
[self resetCollectionViewListCell];
[_contentWrapper addSubview:_titleLabel];
[_contentWrapper addSubview:_detailsTextLabel];
// Image view.
_imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_imageView.autoresizingMask = MDFTrailingMarginAutoresizingMaskForLayoutDirection(
self.mdf_effectiveUserInterfaceLayoutDirection);
[self.contentView addSubview:_imageView];
[self setupConstraints];
}
- (void)resetCollectionViewListCell {
[self updateTitleFont];
_titleLabel.textColor = [UIColor colorWithWhite:0 alpha:kTitleColorOpacity];
_titleLabel.textAlignment = NSTextAlignmentNatural;
_titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_titleLabel.numberOfLines = 1;
[self updateDetailsFont];
_detailsTextLabel.textColor = [UIColor colorWithWhite:0 alpha:kDetailsColorOpacity];
_detailsTextLabel.textAlignment = NSTextAlignmentNatural;
_detailsTextLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_detailsTextLabel.numberOfLines = 3;
}
- (void)setupConstraints {
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
_cellWidthConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:0];
[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:1]
.active = YES;
[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:1]
.active = YES;
_contentWrapper.translatesAutoresizingMaskIntoConstraints = NO;
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
_detailsTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *metrics = @{
@"kTextVerticalPadding" : @(kTextVerticalPadding),
};
NSDictionary *views = @{
@"contentWrapper" : _contentWrapper,
@"titleLabel" : _titleLabel,
@"detailsTextLabel" : _detailsTextLabel,
@"imageView" : _imageView,
};
NSMutableArray *constraints = [[NSMutableArray alloc] init];
_imageLeftPaddingConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0];
_imageLeftPaddingConstraint.active = YES;
_imageRightPaddingConstraint = [NSLayoutConstraint constraintWithItem:_contentWrapper
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_imageView
attribute:NSLayoutAttributeRight
multiplier:1
constant:kImagePadding];
_imageRightPaddingConstraint.active = YES;
_imageWidthConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:0];
_imageWidthConstraint.active = YES;
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:_contentWrapper
attribute:NSLayoutAttributeRight
multiplier:1
constant:kImagePadding]];
[constraints
addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentWrapper]|"
options:0
metrics:nil
views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[titleLabel]|"
options:0
metrics:nil
views:views]];
[constraints
addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[detailsTextLabel]|"
options:0
metrics:nil
views:views]];
[constraints
addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|-(kTextVerticalPadding)-[titleLabel]["
@"detailsTextLabel]-(kTextVerticalPadding)-|"
options:0
metrics:metrics
views:views]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:kImageHeight]];
[NSLayoutConstraint activateConstraints:constraints];
}
- (void)setCellWidth:(CGFloat)width {
_cellWidthConstraint.constant = width;
_cellWidthConstraint.active = YES;
}
- (void)setImage:(UIImage *)image {
_imageView.image = image;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
if (image) {
_imageWidthConstraint.constant = kImageHeight;
_imageLeftPaddingConstraint.constant = kImagePadding;
_imageRightPaddingConstraint.constant = kWithImageRightPadding;
} else {
_imageWidthConstraint.constant = 0;
_imageLeftPaddingConstraint.constant = 0;
_imageRightPaddingConstraint.constant = kImagePadding;
}
}
- (void)prepareForReuse {
[self setImage:nil];
self.titleLabel.text = nil;
self.detailsTextLabel.text = nil;
[self resetCollectionViewListCell];
[_inkView cancelAllAnimationsAnimated:NO];
[super prepareForReuse];
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
[_inkView startTouchBeganAnimationAtPoint:_lastTouch completion:nil];
} else {
[_inkView startTouchEndedAnimationAtPoint:_lastTouch completion:nil];
}
}
- (void)applyTypographyScheme:(id<MDCTypographyScheming>)typographyScheme {
self.titleFont = typographyScheme.subtitle1;
self.detailsFont = typographyScheme.body2;
}
#pragma mark - UIResponder
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
_lastTouch = location;
[super touchesBegan:touches withEvent:event];
}
#pragma mark - Dynamic Type Support
- (BOOL)mdc_adjustsFontForContentSizeCategory {
return _mdc_adjustsFontForContentSizeCategory;
}
- (void)mdc_setAdjustsFontForContentSizeCategory:(BOOL)adjusts {
_mdc_adjustsFontForContentSizeCategory = adjusts;
if (_mdc_adjustsFontForContentSizeCategory) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryDidChange:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
[self updateTitleFont];
[self updateDetailsFont];
}
// Handles UIContentSizeCategoryDidChangeNotifications
- (void)contentSizeCategoryDidChange:(__unused NSNotification *)notification {
[self updateTitleFont];
[self updateDetailsFont];
}
- (void)setTitleFont:(UIFont *)titleFont {
_titleFont = titleFont;
[self updateTitleFont];
}
- (void)updateTitleFont {
if (!_titleFont) {
_titleFont = defaultTitleFont();
}
if (_mdc_adjustsFontForContentSizeCategory) {
_titleLabel.font =
[_titleFont mdc_fontSizedForMaterialTextStyle:MDCFontTextStyleSubheadline
scaledForDynamicType:_mdc_adjustsFontForContentSizeCategory];
} else {
_titleLabel.font = _titleFont;
}
[self setNeedsLayout];
}
- (void)setDetailsFont:(UIFont *)detailsFont {
_detailsFont = detailsFont;
[self updateDetailsFont];
}
- (void)updateDetailsFont {
if (!_detailsFont) {
_detailsFont = defaultDetailsFont();
}
if (_mdc_adjustsFontForContentSizeCategory) {
_detailsTextLabel.font =
[_detailsFont mdc_fontSizedForMaterialTextStyle:MDCFontTextStyleBody1
scaledForDynamicType:_mdc_adjustsFontForContentSizeCategory];
} else {
_detailsTextLabel.font = _detailsFont;
}
[self setNeedsLayout];
}
@end