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/ui/settings/cells/account_control_item.h" |
| 6 |
| 7 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.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 used on the leading and trailing edges of the cell. |
| 16 const CGFloat kHorizontalPadding = 16; |
| 17 |
| 18 // Padding used on the top and bottom edges of the cell. |
| 19 const CGFloat kVerticalPadding = 16; |
| 20 |
| 21 // Padding used between the image and text. |
| 22 const CGFloat kHorizontalPaddingBetweenImageAndText = 10; |
| 23 |
| 24 // Padding between top label and detail label. |
| 25 const CGFloat kVerticalPaddingBetweenLabelAndDetailLabel = 8; |
| 26 } // namespace |
| 27 |
| 28 @implementation AccountControlItem |
| 29 |
| 30 @synthesize image = _image; |
| 31 @synthesize text = _text; |
| 32 @synthesize detailText = _detailText; |
| 33 @synthesize accessoryType = _accessoryType; |
| 34 @synthesize shouldDisplayError = _shouldDisplayError; |
| 35 |
| 36 - (instancetype)initWithType:(NSInteger)type { |
| 37 self = [super initWithType:type]; |
| 38 if (self) { |
| 39 self.cellClass = [AccountControlCell class]; |
| 40 self.accessibilityTraits |= UIAccessibilityTraitButton; |
| 41 } |
| 42 return self; |
| 43 } |
| 44 |
| 45 #pragma mark - CollectionViewItem |
| 46 |
| 47 - (void)configureCell:(AccountControlCell*)cell { |
| 48 [super configureCell:cell]; |
| 49 cell.imageView.image = self.image; |
| 50 cell.accessoryType = self.accessoryType; |
| 51 |
| 52 cell.textLabel.attributedText = |
| 53 [self attributedStringForText:self.text |
| 54 font:[MDCTypography body2Font] |
| 55 color:[[MDCPalette greyPalette] tint900]]; |
| 56 |
| 57 UIColor* detailTextColor = [[MDCPalette greyPalette] tint700]; |
| 58 if (self.shouldDisplayError) { |
| 59 detailTextColor = [[MDCPalette cr_redPalette] tint700]; |
| 60 } |
| 61 cell.detailTextLabel.attributedText = |
| 62 [self attributedStringForText:self.detailText |
| 63 font:[MDCTypography body1Font] |
| 64 color:detailTextColor]; |
| 65 } |
| 66 |
| 67 #pragma mark - Helper methods |
| 68 |
| 69 - (NSAttributedString*)attributedStringForText:(NSString*)text |
| 70 font:(UIFont*)font |
| 71 color:(UIColor*)color { |
| 72 NSMutableParagraphStyle* paragraphStyle = |
| 73 [[NSMutableParagraphStyle alloc] init]; |
| 74 paragraphStyle.lineHeightMultiple = 1.15; |
| 75 return [[NSAttributedString alloc] |
| 76 initWithString:text |
| 77 attributes:@{ |
| 78 NSParagraphStyleAttributeName : paragraphStyle, |
| 79 NSFontAttributeName : font, |
| 80 NSForegroundColorAttributeName : color |
| 81 }]; |
| 82 } |
| 83 |
| 84 @end |
| 85 |
| 86 @interface AccountControlCell () { |
| 87 // Constraint used to set padding between image and text when image exists. |
| 88 NSLayoutConstraint* _textLeadingAnchorConstraint; |
| 89 } |
| 90 @end |
| 91 |
| 92 @implementation AccountControlCell |
| 93 |
| 94 @synthesize imageView = _imageView; |
| 95 @synthesize textLabel = _textLabel; |
| 96 @synthesize detailTextLabel = _detailTextLabel; |
| 97 |
| 98 - (instancetype)initWithFrame:(CGRect)frame { |
| 99 self = [super initWithFrame:frame]; |
| 100 if (self) { |
| 101 self.isAccessibilityElement = YES; |
| 102 [self addSubviews]; |
| 103 [self setDefaultViewStyling]; |
| 104 [self setViewConstraints]; |
| 105 } |
| 106 return self; |
| 107 } |
| 108 |
| 109 // Create and add subviews. |
| 110 - (void)addSubviews { |
| 111 UIView* contentView = self.contentView; |
| 112 contentView.clipsToBounds = YES; |
| 113 |
| 114 _imageView = [[UIImageView alloc] init]; |
| 115 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 116 [contentView addSubview:_imageView]; |
| 117 |
| 118 _textLabel = [[UILabel alloc] init]; |
| 119 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 120 [contentView addSubview:_textLabel]; |
| 121 |
| 122 _detailTextLabel = [[UILabel alloc] init]; |
| 123 _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 124 [contentView addSubview:_detailTextLabel]; |
| 125 } |
| 126 |
| 127 // Set default imageView styling and default font and text colors for labels. |
| 128 - (void)setDefaultViewStyling { |
| 129 _imageView.contentMode = UIViewContentModeCenter; |
| 130 |
| 131 _textLabel.font = [MDCTypography body2Font]; |
| 132 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 133 _detailTextLabel.font = [MDCTypography body1Font]; |
| 134 _detailTextLabel.numberOfLines = 0; |
| 135 } |
| 136 |
| 137 // Set constraints on subviews. |
| 138 - (void)setViewConstraints { |
| 139 UIView* contentView = self.contentView; |
| 140 |
| 141 _textLeadingAnchorConstraint = [_textLabel.leadingAnchor |
| 142 constraintEqualToAnchor:_imageView.trailingAnchor]; |
| 143 |
| 144 [NSLayoutConstraint activateConstraints:@[ |
| 145 // Set leading anchors. |
| 146 [_imageView.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor |
| 147 constant:kHorizontalPadding], |
| 148 [_detailTextLabel.leadingAnchor |
| 149 constraintEqualToAnchor:_textLabel.leadingAnchor], |
| 150 _textLeadingAnchorConstraint, |
| 151 |
| 152 // Set vertical anchors. |
| 153 [_imageView.topAnchor constraintEqualToAnchor:contentView.topAnchor |
| 154 constant:kVerticalPadding], |
| 155 [_textLabel.topAnchor constraintEqualToAnchor:_imageView.topAnchor], |
| 156 [_textLabel.bottomAnchor |
| 157 constraintEqualToAnchor:_detailTextLabel.topAnchor |
| 158 constant:-kVerticalPaddingBetweenLabelAndDetailLabel], |
| 159 [_detailTextLabel.bottomAnchor |
| 160 constraintEqualToAnchor:contentView.bottomAnchor |
| 161 constant:-kVerticalPadding], |
| 162 |
| 163 // Set trailing anchors. |
| 164 [_textLabel.trailingAnchor |
| 165 constraintLessThanOrEqualToAnchor:contentView.trailingAnchor |
| 166 constant:-kHorizontalPadding], |
| 167 [_detailTextLabel.trailingAnchor |
| 168 constraintLessThanOrEqualToAnchor:contentView.trailingAnchor |
| 169 constant:-kHorizontalPadding], |
| 170 ]]; |
| 171 } |
| 172 |
| 173 #pragma mark - UIView |
| 174 |
| 175 - (void)layoutSubviews { |
| 176 [super layoutSubviews]; |
| 177 |
| 178 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 179 // changes, for instance on screen rotation. |
| 180 CGFloat parentWidth = self.contentView.frame.size.width; |
| 181 if (_imageView.image) { |
| 182 _detailTextLabel.preferredMaxLayoutWidth = |
| 183 parentWidth - 2.f * kHorizontalPadding - |
| 184 kHorizontalPaddingBetweenImageAndText - _imageView.image.size.width; |
| 185 _textLeadingAnchorConstraint.constant = |
| 186 kHorizontalPaddingBetweenImageAndText; |
| 187 } else { |
| 188 _detailTextLabel.preferredMaxLayoutWidth = |
| 189 parentWidth - 2.f * kHorizontalPadding; |
| 190 _textLeadingAnchorConstraint.constant = 0; |
| 191 } |
| 192 |
| 193 // Re-layout with the new preferred width to allow the label to adjust its |
| 194 // height. |
| 195 [super layoutSubviews]; |
| 196 } |
| 197 |
| 198 #pragma mark - UICollectionReusableView |
| 199 |
| 200 - (void)prepareForReuse { |
| 201 [super prepareForReuse]; |
| 202 self.imageView.image = nil; |
| 203 self.textLabel.text = nil; |
| 204 self.detailTextLabel.text = nil; |
| 205 self.accessoryType = MDCCollectionViewCellAccessoryNone; |
| 206 self.detailTextLabel.textColor = [[MDCPalette greyPalette] tint700]; |
| 207 } |
| 208 |
| 209 #pragma mark - NSObject(Accessibility) |
| 210 |
| 211 - (NSString*)accessibilityLabel { |
| 212 return [NSString stringWithFormat:@"%@, %@", self.textLabel.text, |
| 213 self.detailTextLabel.text]; |
| 214 } |
| 215 |
| 216 @end |
OLD | NEW |