OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | 5 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
6 | 6 |
7 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" | 7 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
8 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | 8 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
9 | 9 |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
11 #error "This file requires ARC support." | 11 #error "This file requires ARC support." |
12 #endif | 12 #endif |
13 | 13 |
14 namespace { | 14 namespace { |
15 // Padding of the leading and trailing edges of the cell. | 15 // Padding of the leading and trailing edges of the cell. |
16 const CGFloat kHorizontalPadding = 16; | 16 const CGFloat kHorizontalPadding = 16; |
17 | 17 |
18 // Padding of the top and bottom edges of the cell. | 18 // Padding of the top and bottom edges of the cell. |
19 const CGFloat kVerticalPadding = 16; | 19 const CGFloat kVerticalPadding = 16; |
20 | 20 |
21 // Spacing between the image and the text label. | 21 // Spacing between the image and the text labels. |
22 const CGFloat kHorizontalSpacingBetweenImageAndLabel = 8; | 22 const CGFloat kHorizontalSpacingBetweenImageAndLabels = 8; |
| 23 |
| 24 // Spacing between the labels. |
| 25 const CGFloat kVerticalSpacingBetweenLabels = 8; |
23 } // namespace | 26 } // namespace |
24 | 27 |
25 @implementation PaymentsTextItem | 28 @implementation PaymentsTextItem |
26 | 29 |
27 @synthesize text = _text; | 30 @synthesize text = _text; |
| 31 @synthesize detailText = _detailText; |
28 @synthesize image = _image; | 32 @synthesize image = _image; |
| 33 @synthesize accessoryType = _accessoryType; |
29 | 34 |
30 #pragma mark CollectionViewItem | 35 #pragma mark CollectionViewItem |
31 | 36 |
32 - (instancetype)initWithType:(NSInteger)type { | 37 - (instancetype)initWithType:(NSInteger)type { |
33 self = [super initWithType:type]; | 38 self = [super initWithType:type]; |
34 if (self) { | 39 if (self) { |
35 self.cellClass = [PaymentsTextCell class]; | 40 self.cellClass = [PaymentsTextCell class]; |
36 } | 41 } |
37 return self; | 42 return self; |
38 } | 43 } |
39 | 44 |
40 - (void)configureCell:(PaymentsTextCell*)cell { | 45 - (void)configureCell:(PaymentsTextCell*)cell { |
41 [super configureCell:cell]; | 46 [super configureCell:cell]; |
| 47 cell.accessoryType = self.accessoryType; |
42 cell.textLabel.text = self.text; | 48 cell.textLabel.text = self.text; |
| 49 cell.detailTextLabel.text = self.detailText; |
43 cell.imageView.image = self.image; | 50 cell.imageView.image = self.image; |
44 } | 51 } |
45 | 52 |
46 @end | 53 @end |
47 | 54 |
48 @interface PaymentsTextCell () { | 55 @interface PaymentsTextCell () { |
49 NSLayoutConstraint* _textLeadingAnchorConstraint; | 56 NSLayoutConstraint* _labelsLeadingAnchorConstraint; |
50 NSLayoutConstraint* _imageLeadingAnchorConstraint; | 57 NSLayoutConstraint* _imageLeadingAnchorConstraint; |
| 58 UIStackView* _stackView; |
51 } | 59 } |
52 @end | 60 @end |
53 | 61 |
54 @implementation PaymentsTextCell | 62 @implementation PaymentsTextCell |
55 | 63 |
56 @synthesize textLabel = _textLabel; | 64 @synthesize textLabel = _textLabel; |
| 65 @synthesize detailTextLabel = _detailTextLabel; |
57 @synthesize imageView = _imageView; | 66 @synthesize imageView = _imageView; |
58 | 67 |
59 - (instancetype)initWithFrame:(CGRect)frame { | 68 - (instancetype)initWithFrame:(CGRect)frame { |
60 self = [super initWithFrame:frame]; | 69 self = [super initWithFrame:frame]; |
61 if (self) { | 70 if (self) { |
62 self.isAccessibilityElement = YES; | 71 self.isAccessibilityElement = YES; |
63 [self addSubviews]; | 72 [self addSubviews]; |
64 [self setDefaultViewStyling]; | 73 [self setDefaultViewStyling]; |
65 [self setViewConstraints]; | 74 [self setViewConstraints]; |
66 } | 75 } |
67 return self; | 76 return self; |
68 } | 77 } |
69 | 78 |
70 // Create and add subviews. | 79 // Create and add subviews. |
71 - (void)addSubviews { | 80 - (void)addSubviews { |
72 UIView* contentView = self.contentView; | 81 UIView* contentView = self.contentView; |
73 contentView.clipsToBounds = YES; | 82 contentView.clipsToBounds = YES; |
74 | 83 |
| 84 _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]]; |
| 85 _stackView.axis = UILayoutConstraintAxisVertical; |
| 86 _stackView.layoutMarginsRelativeArrangement = YES; |
| 87 _stackView.layoutMargins = UIEdgeInsetsMake( |
| 88 kVerticalPadding, 0, kVerticalPadding, kHorizontalPadding); |
| 89 _stackView.alignment = UIStackViewAlignmentLeading; |
| 90 _stackView.spacing = kVerticalSpacingBetweenLabels; |
| 91 _stackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 92 [contentView addSubview:_stackView]; |
| 93 |
75 _textLabel = [[UILabel alloc] init]; | 94 _textLabel = [[UILabel alloc] init]; |
76 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; | 95 [_stackView addArrangedSubview:_textLabel]; |
77 [contentView addSubview:_textLabel]; | 96 |
| 97 _detailTextLabel = [[UILabel alloc] init]; |
| 98 [_stackView addArrangedSubview:_detailTextLabel]; |
78 | 99 |
79 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | 100 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
80 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | 101 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
81 [contentView addSubview:_imageView]; | 102 [contentView addSubview:_imageView]; |
82 } | 103 } |
83 | 104 |
84 // Set default font and text colors for labels. | 105 // Set default font and text colors for labels. |
85 - (void)setDefaultViewStyling { | 106 - (void)setDefaultViewStyling { |
86 _textLabel.font = [MDCTypography body1Font]; | 107 _textLabel.font = [MDCTypography body2Font]; |
87 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; | 108 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
88 _textLabel.numberOfLines = 0; | 109 _textLabel.numberOfLines = 0; |
89 _textLabel.lineBreakMode = NSLineBreakByWordWrapping; | 110 _textLabel.lineBreakMode = NSLineBreakByWordWrapping; |
| 111 |
| 112 _detailTextLabel.font = [MDCTypography body1Font]; |
| 113 _detailTextLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 114 _detailTextLabel.numberOfLines = 0; |
| 115 _detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping; |
90 } | 116 } |
91 | 117 |
92 // Set constraints on subviews. | 118 // Set constraints on subviews. |
93 - (void)setViewConstraints { | 119 - (void)setViewConstraints { |
94 UIView* contentView = self.contentView; | 120 UIView* contentView = self.contentView; |
95 | 121 |
96 _textLeadingAnchorConstraint = [_textLabel.leadingAnchor | 122 _labelsLeadingAnchorConstraint = [_stackView.leadingAnchor |
97 constraintEqualToAnchor:_imageView.trailingAnchor]; | 123 constraintEqualToAnchor:_imageView.trailingAnchor |
| 124 constant:kHorizontalSpacingBetweenImageAndLabels]; |
98 _imageLeadingAnchorConstraint = [_imageView.leadingAnchor | 125 _imageLeadingAnchorConstraint = [_imageView.leadingAnchor |
99 constraintEqualToAnchor:contentView.leadingAnchor | 126 constraintEqualToAnchor:contentView.leadingAnchor |
100 constant:kHorizontalPadding]; | 127 constant:kHorizontalPadding]; |
101 | 128 |
102 [NSLayoutConstraint activateConstraints:@[ | 129 [NSLayoutConstraint activateConstraints:@[ |
103 [_textLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor | 130 [_stackView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor], |
104 constant:kVerticalPadding], | 131 [_stackView.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor], |
105 [_textLabel.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor | |
106 constant:-kVerticalPadding], | |
107 [_imageView.centerYAnchor | 132 [_imageView.centerYAnchor |
108 constraintEqualToAnchor:contentView.centerYAnchor], | 133 constraintEqualToAnchor:contentView.centerYAnchor], |
109 _textLeadingAnchorConstraint, | 134 _labelsLeadingAnchorConstraint, |
110 _imageLeadingAnchorConstraint, | 135 _imageLeadingAnchorConstraint, |
111 ]]; | 136 ]]; |
112 } | 137 } |
113 | 138 |
114 #pragma mark - UIView | 139 #pragma mark - UIView |
115 | 140 |
116 // Implement -layoutSubviews as per instructions in documentation for | 141 // Implement -layoutSubviews as per instructions in documentation for |
117 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 142 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
118 - (void)layoutSubviews { | 143 - (void)layoutSubviews { |
| 144 _textLabel.hidden = !_textLabel.text; |
| 145 _detailTextLabel.hidden = !_detailTextLabel.text; |
| 146 |
119 [super layoutSubviews]; | 147 [super layoutSubviews]; |
120 | 148 |
121 // Adjust the text label preferredMaxLayoutWidth when the parent's width | 149 // Adjust the text labels' preferredMaxLayoutWidth when the parent's width |
122 // changes, for instance on screen rotation. | 150 // changes, for instance on screen rotation. |
123 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); | 151 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); |
| 152 CGFloat preferredMaxLayoutWidth = parentWidth - (2 * kHorizontalPadding); |
124 if (_imageView.image) { | 153 if (_imageView.image) { |
125 _textLabel.preferredMaxLayoutWidth = | 154 preferredMaxLayoutWidth -= |
126 parentWidth - (2 * kHorizontalPadding) - | 155 kHorizontalSpacingBetweenImageAndLabels - _imageView.image.size.width; |
127 kHorizontalSpacingBetweenImageAndLabel - _imageView.image.size.width; | |
128 _textLeadingAnchorConstraint.constant = | |
129 kHorizontalSpacingBetweenImageAndLabel; | |
130 } else { | 156 } else { |
131 _textLabel.preferredMaxLayoutWidth = parentWidth - (2 * kHorizontalPadding); | 157 _imageLeadingAnchorConstraint.constant = 0; |
132 _textLeadingAnchorConstraint.constant = 0; | 158 _labelsLeadingAnchorConstraint.constant = kHorizontalPadding; |
133 } | 159 } |
| 160 _textLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; |
| 161 _detailTextLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; |
134 | 162 |
135 // Re-layout with the new preferred width to allow the label to adjust its | 163 // Re-layout with the new preferred width to allow the labels to adjust thier |
136 // height. | 164 // height. |
137 [super layoutSubviews]; | 165 [super layoutSubviews]; |
138 } | 166 } |
139 | 167 |
140 #pragma mark - UICollectionReusableView | 168 #pragma mark - UICollectionReusableView |
141 | 169 |
142 - (void)prepareForReuse { | 170 - (void)prepareForReuse { |
143 [super prepareForReuse]; | 171 [super prepareForReuse]; |
144 self.textLabel.text = nil; | 172 self.textLabel.text = nil; |
| 173 self.detailTextLabel.text = nil; |
145 self.imageView.image = nil; | 174 self.imageView.image = nil; |
146 } | 175 } |
147 | 176 |
148 #pragma mark - NSObject(Accessibility) | 177 #pragma mark - NSObject(Accessibility) |
149 | 178 |
150 - (NSString*)accessibilityLabel { | 179 - (NSString*)accessibilityLabel { |
151 return [NSString stringWithFormat:@"%@", self.textLabel.text]; | 180 return [NSString stringWithFormat:@"%@, %@", self.textLabel.text, |
| 181 self.detailTextLabel.text]; |
152 } | 182 } |
153 | 183 |
154 @end | 184 @end |
OLD | NEW |