Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm

Issue 2764783006: Create CollectionViewTextCell (Closed)
Patch Set: Address comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.h "
6
7 #if !defined(__has_feature) || !__has_feature(objc_arc)
8 #error "This file requires ARC support."
9 #endif
10
11 namespace {
12 const CGFloat kMargin = 16;
13 const CGFloat kMinimalHeight = 48;
14 }
15
16 @implementation CollectionViewTextCell
17
18 @synthesize textLabel = _textLabel;
19 @synthesize detailTextLabel = _detailTextLabel;
20
21 - (instancetype)initWithFrame:(CGRect)frame {
22 self = [super initWithFrame:frame];
23 if (self) {
24 UIView* containerView = [[UIView alloc] initWithFrame:CGRectZero];
25 containerView.translatesAutoresizingMaskIntoConstraints = NO;
26 [self.contentView addSubview:containerView];
27
28 _textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
29 _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
30 [containerView addSubview:_textLabel];
31
32 _detailTextLabel = [[UILabel alloc] initWithFrame:CGRectZero];
33 _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
34 [containerView addSubview:_detailTextLabel];
35
36 [NSLayoutConstraint activateConstraints:@[
37 // Total height.
38 // The MDC specs ask for at least 48 pt.
39 [self.contentView.heightAnchor
40 constraintGreaterThanOrEqualToConstant:kMinimalHeight],
41
42 // Container.
43 [containerView.leadingAnchor
44 constraintEqualToAnchor:self.contentView.leadingAnchor
45 constant:kMargin],
46 [containerView.trailingAnchor
47 constraintEqualToAnchor:self.contentView.trailingAnchor
48 constant:-kMargin],
49 [containerView.topAnchor
50 constraintGreaterThanOrEqualToAnchor:self.contentView.topAnchor
51 constant:kMargin],
52 [containerView.bottomAnchor
53 constraintLessThanOrEqualToAnchor:self.contentView.bottomAnchor
54 constant:-kMargin],
55 [containerView.centerYAnchor
56 constraintEqualToAnchor:self.contentView.centerYAnchor],
57
58 // Labels.
59 [_textLabel.leadingAnchor
60 constraintEqualToAnchor:containerView.leadingAnchor],
61 [_textLabel.trailingAnchor
62 constraintLessThanOrEqualToAnchor:containerView.trailingAnchor],
63 [_textLabel.topAnchor constraintEqualToAnchor:containerView.topAnchor],
64 [_textLabel.bottomAnchor
65 constraintEqualToAnchor:_detailTextLabel.topAnchor],
66 [_detailTextLabel.leadingAnchor
67 constraintEqualToAnchor:_textLabel.leadingAnchor],
68 [_detailTextLabel.trailingAnchor
69 constraintLessThanOrEqualToAnchor:containerView.trailingAnchor],
70 [_detailTextLabel.bottomAnchor
71 constraintLessThanOrEqualToAnchor:containerView.bottomAnchor],
72 ]];
73 }
74 return self;
75 }
76
77 // Implement -layoutSubviews as per instructions in documentation for
78 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
79 - (void)layoutSubviews {
80 [super layoutSubviews];
81 // Adjust the text and detailText label preferredMaxLayoutWidth when the
82 // parent's width changes, for instance on screen rotation.
83 CGFloat prefferedMaxLayoutWidth =
84 CGRectGetWidth(self.contentView.frame) - 2 * kMargin;
85 _textLabel.preferredMaxLayoutWidth = prefferedMaxLayoutWidth;
86 _detailTextLabel.preferredMaxLayoutWidth = prefferedMaxLayoutWidth;
87
88 // Re-layout with the new preferred width to allow the label to adjust its
89 // height.
90 [super layoutSubviews];
91 }
92
93 - (void)prepareForReuse {
94 self.textLabel.text = @"";
lpromero 2017/03/23 12:23:35 Use nil
gambard 2017/03/23 15:32:28 Done.
95 self.detailTextLabel.text = @"";
lpromero 2017/03/23 12:23:35 My rule of thumb is to reset everything set in con
gambard 2017/03/23 15:32:28 Acknowledged.
96 }
97
98 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698