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_edit_item.h" |
| 6 |
| 7 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 8 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 9 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 10 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
| 16 namespace { |
| 17 // Padding used on the leading and trailing edges of the cell. |
| 18 const CGFloat kHorizontalPadding = 16; |
| 19 |
| 20 // Padding used on the top and bottom edges of the cell. |
| 21 const CGFloat kVerticalPadding = 16; |
| 22 |
| 23 // Minimum gap between the label and the text field. |
| 24 const CGFloat kLabelAndFieldGap = 5; |
| 25 } // namespace |
| 26 |
| 27 @implementation AutofillEditItem |
| 28 |
| 29 @synthesize textFieldName = _textFieldName; |
| 30 @synthesize textFieldValue = _textFieldValue; |
| 31 @synthesize textFieldEnabled = _textFieldEnabled; |
| 32 @synthesize autofillType = _autofillType; |
| 33 |
| 34 - (instancetype)initWithType:(NSInteger)type { |
| 35 self = [super initWithType:type]; |
| 36 if (self) { |
| 37 self.cellClass = [AutofillEditCell class]; |
| 38 } |
| 39 return self; |
| 40 } |
| 41 |
| 42 #pragma mark CollectionViewItem |
| 43 |
| 44 - (void)configureCell:(AutofillEditCell*)cell { |
| 45 [super configureCell:cell]; |
| 46 cell.textLabel.text = self.textFieldName; |
| 47 cell.textField.text = self.textFieldValue; |
| 48 if (self.textFieldName.length) { |
| 49 cell.textField.accessibilityIdentifier = |
| 50 [NSString stringWithFormat:@"%@_textField", self.textFieldName]; |
| 51 } |
| 52 cell.textField.enabled = self.textFieldEnabled; |
| 53 cell.textField.textColor = self.textFieldEnabled |
| 54 ? [[MDCPalette cr_bluePalette] tint500] |
| 55 : [[MDCPalette greyPalette] tint500]; |
| 56 [cell.textField addTarget:self |
| 57 action:@selector(textFieldChanged:) |
| 58 forControlEvents:UIControlEventEditingChanged]; |
| 59 } |
| 60 |
| 61 #pragma mark - Actions |
| 62 |
| 63 - (void)textFieldChanged:(UITextField*)textField { |
| 64 self.textFieldValue = textField.text; |
| 65 } |
| 66 |
| 67 @end |
| 68 |
| 69 @implementation AutofillEditCell |
| 70 |
| 71 @synthesize textField = _textField; |
| 72 @synthesize textLabel = _textLabel; |
| 73 |
| 74 - (instancetype)initWithFrame:(CGRect)frame { |
| 75 self = [super initWithFrame:frame]; |
| 76 if (self) { |
| 77 self.isAccessibilityElement = YES; |
| 78 self.allowsCellInteractionsWhileEditing = YES; |
| 79 UIView* contentView = self.contentView; |
| 80 |
| 81 _textLabel = [[UILabel alloc] init]; |
| 82 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 83 [contentView addSubview:_textLabel]; |
| 84 |
| 85 _textField = [[UITextField alloc] init]; |
| 86 _textField.translatesAutoresizingMaskIntoConstraints = NO; |
| 87 [contentView addSubview:_textField]; |
| 88 |
| 89 _textLabel.font = |
| 90 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14]; |
| 91 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 92 |
| 93 _textField.font = [[MDFRobotoFontLoader sharedInstance] lightFontOfSize:16]; |
| 94 _textField.textColor = [[MDCPalette greyPalette] tint500]; |
| 95 _textField.autocapitalizationType = UITextAutocapitalizationTypeWords; |
| 96 _textField.autocorrectionType = UITextAutocorrectionTypeNo; |
| 97 _textField.returnKeyType = UIReturnKeyDone; |
| 98 _textField.clearButtonMode = UITextFieldViewModeWhileEditing; |
| 99 _textField.contentVerticalAlignment = |
| 100 UIControlContentVerticalAlignmentCenter; |
| 101 _textField.textAlignment = |
| 102 UseRTLLayout() ? NSTextAlignmentLeft : NSTextAlignmentRight; |
| 103 |
| 104 // Set up the constraints. |
| 105 [NSLayoutConstraint activateConstraints:@[ |
| 106 [_textLabel.leadingAnchor |
| 107 constraintEqualToAnchor:contentView.leadingAnchor |
| 108 constant:kHorizontalPadding], |
| 109 [_textLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor |
| 110 constant:kVerticalPadding], |
| 111 [_textLabel.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor |
| 112 constant:-kVerticalPadding], |
| 113 [_textField.trailingAnchor |
| 114 constraintEqualToAnchor:contentView.trailingAnchor |
| 115 constant:-kHorizontalPadding], |
| 116 [_textField.firstBaselineAnchor |
| 117 constraintEqualToAnchor:_textLabel.firstBaselineAnchor], |
| 118 [_textField.leadingAnchor |
| 119 constraintEqualToAnchor:_textLabel.trailingAnchor |
| 120 constant:kLabelAndFieldGap], |
| 121 ]]; |
| 122 [_textField setContentHuggingPriority:UILayoutPriorityDefaultLow |
| 123 forAxis:UILayoutConstraintAxisHorizontal]; |
| 124 } |
| 125 return self; |
| 126 } |
| 127 |
| 128 - (void)prepareForReuse { |
| 129 [super prepareForReuse]; |
| 130 self.textLabel.text = nil; |
| 131 self.textField.text = nil; |
| 132 self.textField.autocapitalizationType = UITextAutocapitalizationTypeWords; |
| 133 self.textField.autocorrectionType = UITextAutocorrectionTypeNo; |
| 134 self.textField.returnKeyType = UIReturnKeyDone; |
| 135 self.textField.accessibilityIdentifier = nil; |
| 136 self.textField.enabled = NO; |
| 137 self.textField.delegate = nil; |
| 138 [self.textField removeTarget:nil |
| 139 action:nil |
| 140 forControlEvents:UIControlEventAllEvents]; |
| 141 } |
| 142 |
| 143 #pragma mark - Accessibility |
| 144 |
| 145 - (NSString*)accessibilityLabel { |
| 146 return [NSString |
| 147 stringWithFormat:@"%@, %@", self.textLabel.text, self.textField.text]; |
| 148 } |
| 149 |
| 150 @end |
OLD | NEW |