OLD | NEW |
| (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/payments/cells/shipping_address_item.h" | |
6 | |
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" | |
9 | |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
11 #error "This file requires ARC support." | |
12 #endif | |
13 | |
14 namespace { | |
15 // Padding of the leading and trailing edges of the cell. | |
16 const CGFloat kHorizontalPadding = 16; | |
17 | |
18 // Padding of the top and bottom edges of the cell. | |
19 const CGFloat kVerticalPadding = 16; | |
20 | |
21 // Spacing between the labels. | |
22 const CGFloat kVerticalSpacingBetweenLabels = 8; | |
23 } // namespace | |
24 | |
25 @implementation ShippingAddressItem | |
26 | |
27 @synthesize name = _name; | |
28 @synthesize address = _address; | |
29 @synthesize phoneNumber = _phoneNumber; | |
30 @synthesize accessoryType = _accessoryType; | |
31 | |
32 #pragma mark CollectionViewItem | |
33 | |
34 - (instancetype)initWithType:(NSInteger)type { | |
35 self = [super initWithType:type]; | |
36 if (self) { | |
37 self.cellClass = [ShippingAddressCell class]; | |
38 } | |
39 return self; | |
40 } | |
41 | |
42 - (void)configureCell:(ShippingAddressCell*)cell { | |
43 [super configureCell:cell]; | |
44 cell.accessoryType = self.accessoryType; | |
45 cell.nameLabel.text = self.name; | |
46 cell.addressLabel.text = self.address; | |
47 cell.phoneNumberLabel.text = self.phoneNumber; | |
48 } | |
49 | |
50 @end | |
51 | |
52 @implementation ShippingAddressCell | |
53 | |
54 @synthesize nameLabel = _nameLabel; | |
55 @synthesize addressLabel = _addressLabel; | |
56 @synthesize phoneNumberLabel = _phoneNumberLabel; | |
57 | |
58 - (instancetype)initWithFrame:(CGRect)frame { | |
59 self = [super initWithFrame:frame]; | |
60 if (self) { | |
61 self.isAccessibilityElement = YES; | |
62 [self addSubviews]; | |
63 [self setDefaultViewStyling]; | |
64 [self setViewConstraints]; | |
65 } | |
66 return self; | |
67 } | |
68 | |
69 // Create and add subviews. | |
70 - (void)addSubviews { | |
71 UIView* contentView = self.contentView; | |
72 contentView.clipsToBounds = YES; | |
73 | |
74 _nameLabel = [[UILabel alloc] init]; | |
75 _nameLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
76 [contentView addSubview:_nameLabel]; | |
77 | |
78 _addressLabel = [[UILabel alloc] init]; | |
79 _addressLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
80 [contentView addSubview:_addressLabel]; | |
81 | |
82 _phoneNumberLabel = [[UILabel alloc] init]; | |
83 _phoneNumberLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
84 [contentView addSubview:_phoneNumberLabel]; | |
85 } | |
86 | |
87 // Set default font and text colors for labels. | |
88 - (void)setDefaultViewStyling { | |
89 _nameLabel.font = [MDCTypography body2Font]; | |
90 _nameLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
91 _nameLabel.numberOfLines = 0; | |
92 _nameLabel.lineBreakMode = NSLineBreakByWordWrapping; | |
93 | |
94 _addressLabel.font = [MDCTypography body1Font]; | |
95 _addressLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
96 _addressLabel.numberOfLines = 0; | |
97 _addressLabel.lineBreakMode = NSLineBreakByWordWrapping; | |
98 | |
99 _phoneNumberLabel.font = [MDCTypography body1Font]; | |
100 _phoneNumberLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
101 } | |
102 | |
103 // Set constraints on subviews. | |
104 - (void)setViewConstraints { | |
105 UIView* contentView = self.contentView; | |
106 | |
107 [NSLayoutConstraint activateConstraints:@[ | |
108 // Set leading anchors. | |
109 [_nameLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor | |
110 constant:kHorizontalPadding], | |
111 [_addressLabel.leadingAnchor | |
112 constraintEqualToAnchor:_nameLabel.leadingAnchor], | |
113 [_phoneNumberLabel.leadingAnchor | |
114 constraintEqualToAnchor:_addressLabel.leadingAnchor], | |
115 | |
116 // Set vertical anchors. | |
117 [_nameLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor | |
118 constant:kVerticalPadding], | |
119 [_addressLabel.topAnchor | |
120 constraintEqualToAnchor:_nameLabel.bottomAnchor | |
121 constant:kVerticalSpacingBetweenLabels], | |
122 [_addressLabel.bottomAnchor | |
123 constraintEqualToAnchor:_phoneNumberLabel.topAnchor | |
124 constant:-kVerticalSpacingBetweenLabels], | |
125 [_phoneNumberLabel.bottomAnchor | |
126 constraintEqualToAnchor:contentView.bottomAnchor | |
127 constant:-kVerticalPadding], | |
128 | |
129 // Set trailing anchors. | |
130 [_nameLabel.trailingAnchor | |
131 constraintLessThanOrEqualToAnchor:contentView.trailingAnchor | |
132 constant:-kHorizontalPadding], | |
133 [_addressLabel.trailingAnchor | |
134 constraintLessThanOrEqualToAnchor:_nameLabel.trailingAnchor], | |
135 [_phoneNumberLabel.trailingAnchor | |
136 constraintLessThanOrEqualToAnchor:_addressLabel.trailingAnchor], | |
137 ]]; | |
138 } | |
139 | |
140 #pragma mark - UIView | |
141 | |
142 // Implement -layoutSubviews as per instructions in documentation for | |
143 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
144 - (void)layoutSubviews { | |
145 // When the accessory type is None, the content view of the cell (and thus) | |
146 // the labels inside it span larger than when there is a Checkmark accessory | |
147 // type. That means that toggling the accessory type can induce a rewrapping | |
148 // of the texts, which is not visually pleasing. To alleviate that issue | |
149 // always lay out the cell as if there was a Checkmark accessory type. | |
150 // | |
151 // Force the accessory type to Checkmark for the duration of layout. | |
152 MDCCollectionViewCellAccessoryType realAccessoryType = self.accessoryType; | |
153 self.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
154 | |
155 [super layoutSubviews]; | |
156 | |
157 // Adjust labels' preferredMaxLayoutWidth when the parent's width changes, for | |
158 // instance on screen rotation. | |
159 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); | |
160 CGFloat preferredMaxLayoutWidth = parentWidth - (2 * kHorizontalPadding); | |
161 _nameLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | |
162 _addressLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | |
163 _phoneNumberLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | |
164 | |
165 // Re-layout with the new preferred width to allow the label to adjust its | |
166 // height. | |
167 [super layoutSubviews]; | |
168 | |
169 // Restore the real accessory type at the end of the layout. | |
170 self.accessoryType = realAccessoryType; | |
171 } | |
172 | |
173 #pragma mark - UICollectionReusableView | |
174 | |
175 - (void)prepareForReuse { | |
176 [super prepareForReuse]; | |
177 self.nameLabel.text = nil; | |
178 self.addressLabel.text = nil; | |
179 self.phoneNumberLabel.text = nil; | |
180 self.accessoryType = MDCCollectionViewCellAccessoryNone; | |
181 } | |
182 | |
183 #pragma mark - NSObject(Accessibility) | |
184 | |
185 - (NSString*)accessibilityLabel { | |
186 return [NSString stringWithFormat:@"%@, %@, %@", self.nameLabel.text, | |
187 self.addressLabel.text, | |
188 self.phoneNumberLabel.text]; | |
189 } | |
190 | |
191 @end | |
OLD | NEW |