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 top and bottom edges of the cell. |
| 17 const CGFloat kVerticalPadding = 12; |
| 18 |
| 19 // Spacing between the icons. |
| 20 const CGFloat kHorizontalSpacingBetweenIcons = 5; |
| 21 } // namespace |
| 22 |
| 23 @implementation AcceptedPaymentMethodsItem |
| 24 |
| 25 @synthesize message = _message; |
| 26 @synthesize methodTypeIcons = _methodTypeIcons; |
| 27 |
| 28 #pragma mark CollectionViewItem |
| 29 |
| 30 - (instancetype)initWithType:(NSInteger)type { |
| 31 self = [super initWithType:type]; |
| 32 if (self) { |
| 33 self.cellClass = [AcceptedPaymentMethodsCell class]; |
| 34 } |
| 35 return self; |
| 36 } |
| 37 |
| 38 - (void)configureCell:(AcceptedPaymentMethodsCell*)cell { |
| 39 [super configureCell:cell]; |
| 40 cell.messageLabel.text = self.message; |
| 41 |
| 42 NSMutableArray* methodTypeIconViews = [NSMutableArray array]; |
| 43 for (UIImage* methodTypeIcon in self.methodTypeIcons) { |
| 44 UIImageView* methodTypeIconView = |
| 45 [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 46 methodTypeIconView.image = methodTypeIcon; |
| 47 methodTypeIconView.accessibilityLabel = methodTypeIcon.accessibilityLabel; |
| 48 methodTypeIconView.layer.borderColor = |
| 49 [UIColor colorWithWhite:0.9 alpha:1.0].CGColor; |
| 50 methodTypeIconView.layer.borderWidth = 1.0; |
| 51 [methodTypeIconViews addObject:methodTypeIconView]; |
| 52 } |
| 53 cell.methodTypeIconViews = methodTypeIconViews; |
| 54 } |
| 55 |
| 56 @end |
| 57 |
| 58 @implementation AcceptedPaymentMethodsCell { |
| 59 UIStackView* _stackView; |
| 60 } |
| 61 |
| 62 @synthesize messageLabel = _messageLabel; |
| 63 @synthesize methodTypeIconViews = _methodTypeIconViews; |
| 64 |
| 65 - (instancetype)initWithFrame:(CGRect)frame { |
| 66 self = [super initWithFrame:frame]; |
| 67 if (self) { |
| 68 self.isAccessibilityElement = YES; |
| 69 [self addSubviews]; |
| 70 [self setDefaultViewStyling]; |
| 71 [self setViewConstraints]; |
| 72 } |
| 73 return self; |
| 74 } |
| 75 |
| 76 // Create and add subviews. |
| 77 - (void)addSubviews { |
| 78 UIView* contentView = self.contentView; |
| 79 contentView.clipsToBounds = YES; |
| 80 |
| 81 _messageLabel = [[UILabel alloc] init]; |
| 82 _messageLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 83 [contentView addSubview:_messageLabel]; |
| 84 |
| 85 _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]]; |
| 86 _stackView.axis = UILayoutConstraintAxisHorizontal; |
| 87 _stackView.layoutMarginsRelativeArrangement = YES; |
| 88 _stackView.layoutMargins = |
| 89 UIEdgeInsetsMake(kVerticalPadding, 0, kVerticalPadding, 0); |
| 90 _stackView.spacing = kHorizontalSpacingBetweenIcons; |
| 91 _stackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 92 [contentView addSubview:_stackView]; |
| 93 } |
| 94 |
| 95 // Set default font and text colors for labels. |
| 96 - (void)setDefaultViewStyling { |
| 97 _messageLabel.font = [MDCTypography body2Font]; |
| 98 _messageLabel.textColor = [[MDCPalette greyPalette] tint600]; |
| 99 _messageLabel.numberOfLines = 0; |
| 100 _messageLabel.lineBreakMode = NSLineBreakByWordWrapping; |
| 101 } |
| 102 |
| 103 // Set constraints on subviews. |
| 104 - (void)setViewConstraints { |
| 105 [NSLayoutConstraint activateConstraints:@[ |
| 106 [_messageLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor |
| 107 constant:kVerticalPadding], |
| 108 [_messageLabel.leadingAnchor |
| 109 constraintEqualToAnchor:self.contentView.leadingAnchor], |
| 110 [_messageLabel.trailingAnchor |
| 111 constraintEqualToAnchor:self.contentView.trailingAnchor], |
| 112 [_messageLabel.bottomAnchor constraintEqualToAnchor:_stackView.topAnchor], |
| 113 [_stackView.leadingAnchor |
| 114 constraintEqualToAnchor:self.contentView.leadingAnchor], |
| 115 [_stackView.trailingAnchor |
| 116 constraintLessThanOrEqualToAnchor:self.contentView.trailingAnchor], |
| 117 [_stackView.bottomAnchor |
| 118 constraintEqualToAnchor:self.contentView.bottomAnchor], |
| 119 ]]; |
| 120 } |
| 121 |
| 122 - (void)setMethodTypeIconViews:(NSArray<UIImageView*>*)methodTypeIconViews { |
| 123 // Remove the old UIImageViews. |
| 124 for (UIImageView* methodTypeIconView in _methodTypeIconViews) |
| 125 [methodTypeIconView removeFromSuperview]; |
| 126 |
| 127 _methodTypeIconViews = methodTypeIconViews; |
| 128 |
| 129 // Add the new UIImageViews. |
| 130 for (UIImageView* methodTypeIconView in _methodTypeIconViews) |
| 131 [_stackView addArrangedSubview:methodTypeIconView]; |
| 132 } |
| 133 |
| 134 #pragma mark - UIView |
| 135 |
| 136 // Implement -layoutSubviews as per instructions in documentation for |
| 137 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 138 - (void)layoutSubviews { |
| 139 [super layoutSubviews]; |
| 140 |
| 141 // Adjust preferredMaxLayoutWidth of _messageLabel when the parent's width |
| 142 // changes, for instance on screen rotation. |
| 143 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); |
| 144 _messageLabel.preferredMaxLayoutWidth = parentWidth; |
| 145 |
| 146 // Re-layout with the new preferred width to allow the label to adjust its |
| 147 // height. |
| 148 [super layoutSubviews]; |
| 149 } |
| 150 |
| 151 #pragma mark - UICollectionReusableView |
| 152 |
| 153 - (void)prepareForReuse { |
| 154 [super prepareForReuse]; |
| 155 self.messageLabel.text = nil; |
| 156 self.methodTypeIconViews = nil; |
| 157 } |
| 158 |
| 159 #pragma mark - NSObject(Accessibility) |
| 160 |
| 161 - (NSString*)accessibilityLabel { |
| 162 NSMutableString* accessibilityLabel = |
| 163 [NSMutableString stringWithString:self.messageLabel.text]; |
| 164 NSArray* iconsAccessibilityLabels = |
| 165 [self.methodTypeIconViews valueForKeyPath:@"accessibilityLabel"]; |
| 166 NSString* concatenatedAccessibilityLabel = |
| 167 [iconsAccessibilityLabels componentsJoinedByString:@", "]; |
| 168 if (concatenatedAccessibilityLabel.length) { |
| 169 [accessibilityLabel appendFormat:@", %@", concatenatedAccessibilityLabel]; |
| 170 } |
| 171 return accessibilityLabel; |
| 172 } |
| 173 |
| 174 @end |
OLD | NEW |