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/text_and_error_item.h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 9 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.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 used on the leading and trailing edges of the cell. |
| 17 const CGFloat kHorizontalPadding = 16; |
| 18 |
| 19 // Padding used on the top and bottom edges of the cell. |
| 20 const CGFloat kVerticalPadding = 16; |
| 21 |
| 22 // Padding used between the image and text. |
| 23 const CGFloat kHorizontalPaddingBetweenImageAndText = 10; |
| 24 |
| 25 // Error icon fixed horizontal size. |
| 26 const CGFloat kHorizontalErrorIconFixedSize = 25; |
| 27 } // namespace |
| 28 |
| 29 @implementation TextAndErrorItem |
| 30 |
| 31 @synthesize text = _text; |
| 32 @synthesize accessoryType = _accessoryType; |
| 33 @synthesize shouldDisplayError = _shouldDisplayError; |
| 34 @synthesize enabled = _enabled; |
| 35 |
| 36 - (instancetype)initWithType:(NSInteger)type { |
| 37 self = [super initWithType:type]; |
| 38 if (self) { |
| 39 self.cellClass = [TextAndErrorCell class]; |
| 40 self.accessibilityTraits |= UIAccessibilityTraitButton; |
| 41 self.enabled = YES; |
| 42 } |
| 43 return self; |
| 44 } |
| 45 |
| 46 - (void)configureCell:(TextAndErrorCell*)cell { |
| 47 [super configureCell:cell]; |
| 48 cell.textLabel.text = self.text; |
| 49 cell.accessoryType = self.accessoryType; |
| 50 cell.accessibilityLabel = self.text; |
| 51 if (self.shouldDisplayError) { |
| 52 cell.errorIcon.image = [UIImage imageNamed:@"settings_error"]; |
| 53 } else { |
| 54 cell.errorIcon.image = nil; |
| 55 } |
| 56 UIImageView* accessoryImage = |
| 57 base::mac::ObjCCastStrict<UIImageView>(cell.accessoryView); |
| 58 accessoryImage.image = [accessoryImage.image |
| 59 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| 60 if (self.isEnabled) { |
| 61 cell.textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 62 [cell setUserInteractionEnabled:YES]; |
| 63 [accessoryImage setTintColor:[[MDCPalette greyPalette] tint900]]; |
| 64 } else { |
| 65 [accessoryImage setTintColor:[[[MDCPalette greyPalette] tint500] |
| 66 colorWithAlphaComponent:0.5]]; |
| 67 cell.textLabel.textColor = [[MDCPalette greyPalette] tint500]; |
| 68 [cell setUserInteractionEnabled:NO]; |
| 69 } |
| 70 } |
| 71 |
| 72 @end |
| 73 |
| 74 @interface TextAndErrorCell () { |
| 75 // Constraint used to set the errorIcon width depending on its existence. |
| 76 NSLayoutConstraint* _errorIconWidthConstraint; |
| 77 } |
| 78 |
| 79 @end |
| 80 |
| 81 @implementation TextAndErrorCell |
| 82 |
| 83 @synthesize textLabel = _textLabel; |
| 84 @synthesize errorIcon = _errorIcon; |
| 85 |
| 86 - (instancetype)initWithFrame:(CGRect)frame { |
| 87 self = [super initWithFrame:frame]; |
| 88 if (self) { |
| 89 self.contentView.clipsToBounds = YES; |
| 90 _textLabel = [[UILabel alloc] init]; |
| 91 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 92 _textLabel.font = |
| 93 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14]; |
| 94 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 95 _textLabel.numberOfLines = 0; |
| 96 [self.contentView addSubview:_textLabel]; |
| 97 _errorIcon = [[UIImageView alloc] init]; |
| 98 _errorIcon.translatesAutoresizingMaskIntoConstraints = NO; |
| 99 [self.contentView addSubview:_errorIcon]; |
| 100 [self setConstraints]; |
| 101 } |
| 102 return self; |
| 103 } |
| 104 |
| 105 - (void)setConstraints { |
| 106 UIView* contentView = self.contentView; |
| 107 |
| 108 _errorIconWidthConstraint = [_errorIcon.widthAnchor |
| 109 constraintEqualToConstant:kHorizontalErrorIconFixedSize]; |
| 110 |
| 111 [NSLayoutConstraint activateConstraints:@[ |
| 112 [_textLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor |
| 113 constant:kHorizontalPadding], |
| 114 [_textLabel.trailingAnchor |
| 115 constraintEqualToAnchor:_errorIcon.leadingAnchor |
| 116 constant:-kHorizontalPaddingBetweenImageAndText], |
| 117 [_textLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor |
| 118 constant:kVerticalPadding], |
| 119 [_textLabel.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor |
| 120 constant:-kVerticalPadding], |
| 121 [_errorIcon.trailingAnchor |
| 122 constraintEqualToAnchor:contentView.trailingAnchor |
| 123 constant:-kHorizontalPadding], |
| 124 [_errorIcon.centerYAnchor |
| 125 constraintEqualToAnchor:contentView.centerYAnchor], |
| 126 _errorIconWidthConstraint, |
| 127 ]]; |
| 128 } |
| 129 |
| 130 // Implement -layoutSubviews as per instructions in documentation for |
| 131 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 132 - (void)layoutSubviews { |
| 133 [super layoutSubviews]; |
| 134 |
| 135 // Adjust the text and detailText label preferredMaxLayoutWidth when the |
| 136 // parent's width |
| 137 // changes, for instance on screen rotation. |
| 138 if (_errorIcon.image) { |
| 139 _errorIconWidthConstraint.constant = kHorizontalErrorIconFixedSize; |
| 140 _textLabel.preferredMaxLayoutWidth = |
| 141 CGRectGetWidth(self.contentView.frame) - |
| 142 CGRectGetWidth(_errorIcon.frame) - 2 * kHorizontalPadding - |
| 143 kHorizontalPaddingBetweenImageAndText; |
| 144 } else { |
| 145 _errorIconWidthConstraint.constant = 0; |
| 146 _textLabel.preferredMaxLayoutWidth = |
| 147 CGRectGetWidth(self.contentView.frame) - 2 * kHorizontalPadding; |
| 148 } |
| 149 |
| 150 // Re-layout with the new preferred width to allow the label to adjust its |
| 151 // height. |
| 152 [super layoutSubviews]; |
| 153 } |
| 154 |
| 155 - (void)prepareForReuse { |
| 156 [super prepareForReuse]; |
| 157 self.textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 158 [self setUserInteractionEnabled:YES]; |
| 159 UIImageView* accessoryImage = |
| 160 base::mac::ObjCCastStrict<UIImageView>(self.accessoryView); |
| 161 accessoryImage.image = [accessoryImage.image |
| 162 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| 163 [accessoryImage setTintColor:[[MDCPalette greyPalette] tint900]]; |
| 164 } |
| 165 |
| 166 @end |
OLD | NEW |