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

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

Issue 2588713002: Upstream Chrome on iOS source code [4/11]. (Closed)
Patch Set: Created 4 years 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 2016 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_detail_item .h"
6
7 #include <algorithm>
8
9 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat erialPalettes.h"
10 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
11
12 #if !defined(__has_feature) || !__has_feature(objc_arc)
13 #error "This file requires ARC support."
14 #endif
15
16 namespace {
17
18 // Padding used on the leading and trailing edges of the cell and between the
19 // two labels.
20 const CGFloat kHorizontalPadding = 16;
21
22 // Minimum proportion of the available width to guarantee to the main and detail
23 // labels.
24 const CGFloat kMinTextWidthRatio = 0.75f;
25 const CGFloat kMinDetailTextWidthRatio = 0.25f;
26 }
27
28 @implementation CollectionViewDetailItem
29
30 @synthesize accessoryType = _accessoryType;
31 @synthesize text = _text;
32 @synthesize detailText = _detailText;
33
34 - (instancetype)initWithType:(NSInteger)type {
35 self = [super initWithType:type];
36 if (self) {
37 self.cellClass = [CollectionViewDetailCell class];
38 }
39 return self;
40 }
41
42 #pragma mark CollectionViewItem
43
44 - (void)configureCell:(CollectionViewDetailCell*)cell {
45 [super configureCell:cell];
46 cell.accessoryType = self.accessoryType;
47 cell.textLabel.text = self.text;
48 cell.detailTextLabel.text = self.detailText;
49 }
50
51 @end
52
53 @implementation CollectionViewDetailCell {
54 NSLayoutConstraint* _textLabelWidthConstraint;
55 NSLayoutConstraint* _detailTextLabelWidthConstraint;
56 }
57
58 @synthesize textLabel = _textLabel;
59 @synthesize detailTextLabel = _detailTextLabel;
60
61 - (instancetype)initWithFrame:(CGRect)frame {
62 self = [super initWithFrame:frame];
63 if (self) {
64 self.isAccessibilityElement = YES;
65 UIView* contentView = self.contentView;
66
67 _textLabel = [[UILabel alloc] init];
68 _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
69 _textLabel.backgroundColor = [UIColor clearColor];
70 [contentView addSubview:_textLabel];
71
72 _textLabel.font =
73 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
74 _textLabel.textColor = [[MDCPalette greyPalette] tint900];
75
76 _detailTextLabel = [[UILabel alloc] init];
77 _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
78 _detailTextLabel.backgroundColor = [UIColor clearColor];
79 [contentView addSubview:_detailTextLabel];
80
81 _detailTextLabel.font =
82 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:14];
83 _detailTextLabel.textColor = [[MDCPalette greyPalette] tint500];
84
85 // Set up the width constraints. They are activated here and updated in
86 // layoutSubviews.
87 _textLabelWidthConstraint =
88 [_textLabel.widthAnchor constraintEqualToConstant:0];
89 _detailTextLabelWidthConstraint =
90 [_detailTextLabel.widthAnchor constraintEqualToConstant:0];
91
92 [NSLayoutConstraint activateConstraints:@[
93 // Fix the leading and trailing edges of the text labels.
94 [_textLabel.leadingAnchor
95 constraintEqualToAnchor:contentView.leadingAnchor
96 constant:kHorizontalPadding],
97 [_detailTextLabel.trailingAnchor
98 constraintEqualToAnchor:contentView.trailingAnchor
99 constant:-kHorizontalPadding],
100
101 // Center the text label vertically and align the baselines of the two
102 // text labels.
103 [_textLabel.centerYAnchor
104 constraintEqualToAnchor:contentView.centerYAnchor],
105 [_detailTextLabel.firstBaselineAnchor
106 constraintEqualToAnchor:_textLabel.firstBaselineAnchor],
107
108 _textLabelWidthConstraint,
109 _detailTextLabelWidthConstraint,
110 ]];
111 }
112 return self;
113 }
114
115 // Updates the layout constraints of the text labels and then calls the
116 // superclass's implementation of layoutSubviews which can then take account of
117 // the new constraints.
118 - (void)layoutSubviews {
119 [super layoutSubviews];
120
121 // Size the labels in order to determine how much width they want.
122 [self.textLabel sizeToFit];
123 [self.detailTextLabel sizeToFit];
124
125 // Update the width constraints.
126 _textLabelWidthConstraint.constant = self.textLabelTargetWidth;
127 _detailTextLabelWidthConstraint.constant = self.detailTextLabelTargetWidth;
128
129 // Now invoke the layout.
130 [super layoutSubviews];
131 }
132
133 - (CGFloat)textLabelTargetWidth {
134 CGFloat availableWidth =
135 self.contentView.bounds.size.width - (3 * kHorizontalPadding);
136 CGFloat textLabelWidth = self.textLabel.frame.size.width;
137 CGFloat detailTextLabelWidth = self.detailTextLabel.frame.size.width;
138
139 if (textLabelWidth + detailTextLabelWidth <= availableWidth)
140 return textLabelWidth;
141
142 return std::max(
143 availableWidth - detailTextLabelWidth,
144 std::min(availableWidth * kMinTextWidthRatio, textLabelWidth));
145 }
146
147 - (CGFloat)detailTextLabelTargetWidth {
148 CGFloat availableWidth =
149 self.contentView.bounds.size.width - (3 * kHorizontalPadding);
150 CGFloat textLabelWidth = self.textLabel.frame.size.width;
151 CGFloat detailTextLabelWidth = self.detailTextLabel.frame.size.width;
152
153 if (textLabelWidth + detailTextLabelWidth <= availableWidth)
154 return detailTextLabelWidth;
155
156 return std::max(availableWidth - textLabelWidth,
157 std::min(availableWidth * kMinDetailTextWidthRatio,
158 detailTextLabelWidth));
159 }
160
161 - (NSString*)accessibilityLabel {
162 return self.textLabel.text;
163 }
164
165 - (NSString*)accessibilityValue {
166 return self.detailTextLabel.text;
167 }
168
169 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698