OLD | NEW |
---|---|
(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/payments/cells/accepted_payment_methods_item.h" | |
6 | |
7 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | |
8 #import "ios/chrome/browser/ui/uikit_ui_util.h" | |
9 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h" | |
10 | |
11 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
12 #error "This file requires ARC support." | |
13 #endif | |
14 | |
15 namespace { | |
16 // Padding of the leading and trailing edges of the cell. | |
17 const CGFloat kHorizontalPadding = 16; | |
18 | |
19 // Padding of the top and bottom edges of the cell. | |
20 const CGFloat kVerticalPadding = 12; | |
21 | |
22 // Spacing between the icons. | |
23 const CGFloat kHorizontalSpacingBetweenIcons = 8; | |
24 } // namespace | |
25 | |
26 @implementation AcceptedPaymentMethodsItem | |
27 | |
28 @synthesize message = _message; | |
29 @synthesize methodTypeIcons = _methodTypeIcons; | |
30 | |
31 #pragma mark CollectionViewItem | |
32 | |
33 - (instancetype)initWithType:(NSInteger)type { | |
34 self = [super initWithType:type]; | |
35 if (self) { | |
36 self.cellClass = [AcceptedPaymentMethodsCell class]; | |
37 } | |
38 return self; | |
39 } | |
40 | |
41 - (void)configureCell:(AcceptedPaymentMethodsCell*)cell { | |
42 [super configureCell:cell]; | |
43 cell.messageLabel.text = self.message; | |
44 | |
45 NSMutableArray* methodTypeIconViews = [NSMutableArray array]; | |
46 for (UIImage* methodTypeIcon in self.methodTypeIcons) { | |
47 UIImageView* methodTypeIconView = | |
48 [[UIImageView alloc] initWithFrame:CGRectZero]; | |
49 methodTypeIconView.image = methodTypeIcon; | |
50 methodTypeIconView.layer.borderColor = | |
51 [UIColor colorWithWhite:0.9 alpha:1.0].CGColor; | |
52 methodTypeIconView.layer.borderWidth = 1.0; | |
53 [methodTypeIconViews addObject:methodTypeIconView]; | |
54 } | |
55 cell.methodTypeIconViews = methodTypeIconViews; | |
56 } | |
57 | |
58 @end | |
59 | |
60 @implementation AcceptedPaymentMethodsCell { | |
61 UIStackView* _stackView; | |
lpromero
2017/04/20 16:59:51
How does the stack view behave if there are more e
Moe
2017/04/21 14:14:14
It would squeeze the ones that don't fit. I adjust
| |
62 } | |
63 | |
64 @synthesize messageLabel = _messageLabel; | |
65 @synthesize methodTypeIconViews = _methodTypeIconViews; | |
66 | |
67 - (instancetype)initWithFrame:(CGRect)frame { | |
68 self = [super initWithFrame:frame]; | |
69 if (self) { | |
70 self.isAccessibilityElement = YES; | |
71 [self addSubviews]; | |
72 [self setDefaultViewStyling]; | |
73 [self setViewConstraints]; | |
74 } | |
75 return self; | |
76 } | |
77 | |
78 // Create and add subviews. | |
79 - (void)addSubviews { | |
80 UIView* contentView = self.contentView; | |
81 contentView.clipsToBounds = YES; | |
82 | |
83 _messageLabel = [[UILabel alloc] init]; | |
84 _messageLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
85 [contentView addSubview:_messageLabel]; | |
86 | |
87 _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]]; | |
88 _stackView.axis = UILayoutConstraintAxisHorizontal; | |
89 _stackView.layoutMarginsRelativeArrangement = YES; | |
90 _stackView.layoutMargins = | |
91 UIEdgeInsetsMake(kVerticalPadding, kHorizontalPadding, kVerticalPadding, | |
92 kHorizontalPadding); | |
93 _stackView.spacing = kHorizontalSpacingBetweenIcons; | |
94 _stackView.translatesAutoresizingMaskIntoConstraints = NO; | |
95 [contentView addSubview:_stackView]; | |
96 } | |
97 | |
98 // Set default font and text colors for labels. | |
99 - (void)setDefaultViewStyling { | |
100 _messageLabel.font = [MDCTypography body2Font]; | |
101 _messageLabel.textColor = [[MDCPalette greyPalette] tint600]; | |
102 _messageLabel.numberOfLines = 0; | |
103 _messageLabel.lineBreakMode = NSLineBreakByWordWrapping; | |
104 } | |
105 | |
106 // Set constraints on subviews. | |
107 - (void)setViewConstraints { | |
108 [NSLayoutConstraint activateConstraints:@[ | |
109 [_messageLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor | |
110 constant:kVerticalPadding], | |
111 [_messageLabel.leadingAnchor | |
112 constraintEqualToAnchor:self.contentView.leadingAnchor | |
113 constant:kHorizontalPadding], | |
114 [_messageLabel.trailingAnchor | |
115 constraintEqualToAnchor:self.contentView.trailingAnchor | |
116 constant:-kHorizontalPadding], | |
117 [_messageLabel.bottomAnchor constraintEqualToAnchor:_stackView.topAnchor], | |
118 [_stackView.leadingAnchor | |
119 constraintEqualToAnchor:self.contentView.leadingAnchor], | |
120 [_stackView.trailingAnchor | |
121 constraintLessThanOrEqualToAnchor:self.contentView.trailingAnchor], | |
122 [_stackView.bottomAnchor | |
123 constraintEqualToAnchor:self.contentView.bottomAnchor], | |
124 ]]; | |
125 } | |
126 | |
127 #pragma mark - UIView | |
128 | |
129 // Implement -layoutSubviews as per instructions in documentation for | |
130 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
131 - (void)layoutSubviews { | |
132 for (UIImageView* methodTypeIconView in _methodTypeIconViews) { | |
133 [_stackView addArrangedSubview:methodTypeIconView]; | |
lpromero
2017/04/20 16:59:51
This might be very costly to add views here. I thi
Moe
2017/04/21 14:14:14
Done.
| |
134 } | |
135 | |
136 [super layoutSubviews]; | |
137 | |
138 // Adjust preferredMaxLayoutWidth of _messageLabel when the parent's width | |
139 // changes, for instance on screen rotation. | |
140 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); | |
141 CGFloat preferredMaxLayoutWidth = parentWidth - (2 * kHorizontalPadding); | |
142 _messageLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | |
143 | |
144 // Re-layout with the new preferred width to allow the label to adjust its | |
145 // height. | |
146 [super layoutSubviews]; | |
147 } | |
148 | |
149 #pragma mark - UICollectionReusableView | |
150 | |
151 - (void)prepareForReuse { | |
152 [super prepareForReuse]; | |
153 self.messageLabel.text = nil; | |
154 for (UIImageView* methodTypeIconView in self.methodTypeIconViews) | |
155 [methodTypeIconView removeFromSuperview]; | |
lpromero
2017/04/20 16:59:51
If you override the setter, you could do this ther
Moe
2017/04/21 14:14:14
Done.
| |
156 self.methodTypeIconViews = nil; | |
157 } | |
158 | |
159 #pragma mark - NSObject(Accessibility) | |
160 | |
161 - (NSString*)accessibilityLabel { | |
162 return [NSString stringWithFormat:@"%@", self.messageLabel.text]; | |
lpromero
2017/04/20 16:59:51
This is insufficient for accessibility. To fix, yo
Moe
2017/04/21 14:14:14
Done.
PS. UIImage's accessibility label doesn't se
lpromero
2017/04/21 14:49:41
Good to know. Thank you for the warning.
| |
163 } | |
164 | |
165 @end | |
OLD | NEW |