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/autofill_data_item.h" |
| 6 |
| 7 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 8 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.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 // Padding used on the top and bottom edges of the cell. |
| 18 const CGFloat kVerticalPadding = 16; |
| 19 } |
| 20 |
| 21 @implementation AutofillDataItem |
| 22 |
| 23 @synthesize deletable = _deletable; |
| 24 @synthesize GUID = _GUID; |
| 25 @synthesize text = _text; |
| 26 @synthesize leadingDetailText = _leadingDetailText; |
| 27 @synthesize trailingDetailText = _trailingDetailText; |
| 28 @synthesize accessoryType = _accessoryType; |
| 29 |
| 30 - (instancetype)initWithType:(NSInteger)type { |
| 31 self = [super initWithType:type]; |
| 32 if (self) { |
| 33 self.cellClass = [AutofillDataCell class]; |
| 34 } |
| 35 return self; |
| 36 } |
| 37 |
| 38 #pragma mark - CollectionViewItem |
| 39 |
| 40 - (void)configureCell:(AutofillDataCell*)cell { |
| 41 [super configureCell:cell]; |
| 42 cell.textLabel.text = self.text; |
| 43 cell.leadingDetailTextLabel.text = self.leadingDetailText; |
| 44 cell.trailingDetailTextLabel.text = self.trailingDetailText; |
| 45 cell.accessoryType = self.accessoryType; |
| 46 } |
| 47 |
| 48 @end |
| 49 |
| 50 @implementation AutofillDataCell { |
| 51 NSLayoutConstraint* _textLabelWidthConstraint; |
| 52 } |
| 53 |
| 54 @synthesize textLabel = _textLabel; |
| 55 @synthesize leadingDetailTextLabel = _leadingDetailTextLabel; |
| 56 @synthesize trailingDetailTextLabel = _trailingDetailTextLabel; |
| 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 // Creates and adds subviews. |
| 70 - (void)addSubviews { |
| 71 UIView* contentView = self.contentView; |
| 72 |
| 73 _textLabel = [[UILabel alloc] init]; |
| 74 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 75 [contentView addSubview:_textLabel]; |
| 76 |
| 77 _leadingDetailTextLabel = [[UILabel alloc] init]; |
| 78 _leadingDetailTextLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 79 [contentView addSubview:_leadingDetailTextLabel]; |
| 80 |
| 81 _trailingDetailTextLabel = [[UILabel alloc] init]; |
| 82 _trailingDetailTextLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 83 [contentView addSubview:_trailingDetailTextLabel]; |
| 84 } |
| 85 |
| 86 // Sets default font and text colors for labels. |
| 87 - (void)setDefaultViewStyling { |
| 88 _textLabel.font = [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14]; |
| 89 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 90 _textLabel.numberOfLines = 0; |
| 91 _textLabel.lineBreakMode = NSLineBreakByWordWrapping; |
| 92 |
| 93 _leadingDetailTextLabel.font = |
| 94 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:14]; |
| 95 _leadingDetailTextLabel.textColor = [[MDCPalette greyPalette] tint500]; |
| 96 _leadingDetailTextLabel.numberOfLines = 0; |
| 97 _leadingDetailTextLabel.lineBreakMode = NSLineBreakByWordWrapping; |
| 98 |
| 99 _trailingDetailTextLabel.font = |
| 100 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:12]; |
| 101 _trailingDetailTextLabel.textColor = [[MDCPalette greyPalette] tint500]; |
| 102 } |
| 103 |
| 104 // Sets constraints on subviews. |
| 105 - (void)setViewConstraints { |
| 106 UIView* contentView = self.contentView; |
| 107 |
| 108 // Set up the width constraint for the text label. It is activated here |
| 109 // and updated in layoutSubviews. |
| 110 _textLabelWidthConstraint = |
| 111 [_textLabel.widthAnchor constraintEqualToConstant:0]; |
| 112 |
| 113 [NSLayoutConstraint activateConstraints:@[ |
| 114 // Set horizontal anchors. |
| 115 [_textLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor |
| 116 constant:kHorizontalPadding], |
| 117 [_leadingDetailTextLabel.leadingAnchor |
| 118 constraintEqualToAnchor:_textLabel.leadingAnchor], |
| 119 [_trailingDetailTextLabel.trailingAnchor |
| 120 constraintEqualToAnchor:contentView.trailingAnchor |
| 121 constant:-kHorizontalPadding], |
| 122 |
| 123 // Set width anchors. |
| 124 [_leadingDetailTextLabel.widthAnchor |
| 125 constraintEqualToAnchor:_textLabel.widthAnchor], |
| 126 _textLabelWidthConstraint, |
| 127 |
| 128 // Set vertical anchors. |
| 129 [_textLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor |
| 130 constant:kVerticalPadding], |
| 131 [_leadingDetailTextLabel.topAnchor |
| 132 constraintEqualToAnchor:_textLabel.bottomAnchor], |
| 133 [_leadingDetailTextLabel.bottomAnchor |
| 134 constraintEqualToAnchor:contentView.bottomAnchor |
| 135 constant:-kVerticalPadding], |
| 136 [_trailingDetailTextLabel.centerYAnchor |
| 137 constraintEqualToAnchor:contentView.centerYAnchor], |
| 138 ]]; |
| 139 } |
| 140 |
| 141 // Implement -layoutSubviews as per instructions in documentation for |
| 142 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 143 - (void)layoutSubviews { |
| 144 [super layoutSubviews]; |
| 145 |
| 146 // Size the trailing detail label to determine how much width it wants. |
| 147 [self.trailingDetailTextLabel sizeToFit]; |
| 148 |
| 149 // Update the text label's width constraint. |
| 150 CGFloat availableWidth = |
| 151 CGRectGetWidth(self.contentView.bounds) - (3 * kHorizontalPadding); |
| 152 CGFloat trailingDetailLabelWidth = |
| 153 CGRectGetWidth(self.trailingDetailTextLabel.frame); |
| 154 _textLabelWidthConstraint.constant = |
| 155 availableWidth - trailingDetailLabelWidth; |
| 156 |
| 157 [super layoutSubviews]; |
| 158 } |
| 159 |
| 160 #pragma mark - UICollectionReusableView |
| 161 |
| 162 - (void)prepareForReuse { |
| 163 [super prepareForReuse]; |
| 164 self.textLabel.text = nil; |
| 165 self.leadingDetailTextLabel.text = nil; |
| 166 self.trailingDetailTextLabel.text = nil; |
| 167 self.accessoryType = MDCCollectionViewCellAccessoryNone; |
| 168 } |
| 169 |
| 170 #pragma mark - NSObject(Accessibility) |
| 171 |
| 172 - (NSString*)accessibilityLabel { |
| 173 if (self.trailingDetailTextLabel.text) { |
| 174 return [NSString stringWithFormat:@"%@, %@, %@", self.textLabel.text, |
| 175 self.leadingDetailTextLabel.text, |
| 176 self.trailingDetailTextLabel.text]; |
| 177 } |
| 178 return [NSString stringWithFormat:@"%@, %@", self.textLabel.text, |
| 179 self.leadingDetailTextLabel.text]; |
| 180 } |
| 181 |
| 182 @end |
OLD | NEW |