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/byo_textfield_item.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." |
| 11 #endif |
| 12 |
| 13 namespace { |
| 14 // Padding used on the leading and trailing edges of the cell. |
| 15 const CGFloat kHorizontalPadding = 16; |
| 16 } // namespace |
| 17 |
| 18 @interface BYOTextFieldCell () |
| 19 |
| 20 // The text field installed in this cell. |
| 21 @property(nonatomic, strong) UITextField* textField; |
| 22 |
| 23 // The constraints set on the textfield. |
| 24 @property(nonatomic, strong) NSArray<NSLayoutConstraint*>* textFieldConstraints; |
| 25 |
| 26 // Sets up the text field and displays it inside the cell. |
| 27 - (void)installTextField:(UITextField*)textField; |
| 28 |
| 29 @end |
| 30 |
| 31 @implementation BYOTextFieldItem |
| 32 |
| 33 @synthesize textField = _textField; |
| 34 |
| 35 - (instancetype)initWithType:(NSInteger)type { |
| 36 self = [super initWithType:type]; |
| 37 if (self) { |
| 38 self.cellClass = [BYOTextFieldCell class]; |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 - (void)configureCell:(BYOTextFieldCell*)cell { |
| 44 [super configureCell:cell]; |
| 45 [cell installTextField:self.textField]; |
| 46 } |
| 47 |
| 48 @end |
| 49 |
| 50 @implementation BYOTextFieldCell |
| 51 |
| 52 @synthesize textField = _textField; |
| 53 @synthesize textFieldConstraints = _textFieldConstraints; |
| 54 |
| 55 - (void)installTextField:(UITextField*)textField { |
| 56 // Make sure not text field is still installed. |
| 57 [self uninstallTextField]; |
| 58 |
| 59 // If there is no text field to install, return. |
| 60 if (!textField) { |
| 61 return; |
| 62 } |
| 63 |
| 64 // Store the text field. |
| 65 self.textField = textField; |
| 66 |
| 67 // Add it to the content view. |
| 68 textField.translatesAutoresizingMaskIntoConstraints = NO; |
| 69 UIView* contentView = self.contentView; |
| 70 [contentView addSubview:textField]; |
| 71 |
| 72 // Store the constraints. |
| 73 self.textFieldConstraints = @[ |
| 74 [textField.topAnchor constraintEqualToAnchor:contentView.topAnchor], |
| 75 [textField.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor], |
| 76 [textField.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor |
| 77 constant:kHorizontalPadding], |
| 78 [textField.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor |
| 79 constant:-kHorizontalPadding], |
| 80 ]; |
| 81 |
| 82 // Activate the constraints. |
| 83 [NSLayoutConstraint activateConstraints:self.textFieldConstraints]; |
| 84 } |
| 85 |
| 86 - (void)uninstallTextField { |
| 87 // Deactivate the constraints. |
| 88 [NSLayoutConstraint deactivateConstraints:self.textFieldConstraints]; |
| 89 // Release the constraints. |
| 90 self.textFieldConstraints = nil; |
| 91 // Remove the text field from the content view. |
| 92 [self.textField removeFromSuperview]; |
| 93 // Release the text field. |
| 94 self.textField = nil; |
| 95 } |
| 96 |
| 97 - (void)prepareForReuse { |
| 98 [super prepareForReuse]; |
| 99 [self uninstallTextField]; |
| 100 } |
| 101 |
| 102 @end |
OLD | NEW |