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/password_details_item.h" |
| 6 |
| 7 #import <CoreGraphics/CoreGraphics.h> |
| 8 #import <UIKit/UIKit.h> |
| 9 |
| 10 #include "ios/chrome/grit/ios_strings.h" |
| 11 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 12 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 13 #include "ui/base/l10n/l10n_util_mac.h" |
| 14 |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 16 #error "This file requires ARC support." |
| 17 #endif |
| 18 |
| 19 namespace { |
| 20 // Padding used on the leading and trailing edges of the cell. |
| 21 const CGFloat kHorizontalPadding = 16; |
| 22 |
| 23 // Padding used on the top and bottom edges of the cell. |
| 24 const CGFloat kVerticalPadding = 16; |
| 25 } // namespace |
| 26 |
| 27 @implementation PasswordDetailsItem |
| 28 |
| 29 @synthesize text = _text; |
| 30 @synthesize showingText = _showingText; |
| 31 |
| 32 - (instancetype)initWithType:(NSInteger)type { |
| 33 self = [super initWithType:type]; |
| 34 if (self) { |
| 35 self.cellClass = [PasswordDetailsCell class]; |
| 36 } |
| 37 return self; |
| 38 } |
| 39 |
| 40 - (void)configureCell:(PasswordDetailsCell*)cell { |
| 41 [super configureCell:cell]; |
| 42 if (self.showingText) { |
| 43 cell.textLabel.text = self.text; |
| 44 cell.accessibilityLabel = self.text; |
| 45 } else { |
| 46 NSString* obscuredText = [@"" stringByPaddingToLength:[self.text length] |
| 47 withString:@"●" |
| 48 startingAtIndex:0]; |
| 49 cell.textLabel.text = obscuredText; |
| 50 cell.accessibilityLabel = |
| 51 l10n_util::GetNSString(IDS_IOS_SETTINGS_PASSWORD_HIDDEN_LABEL); |
| 52 } |
| 53 } |
| 54 |
| 55 @end |
| 56 |
| 57 @implementation PasswordDetailsCell |
| 58 |
| 59 @synthesize textLabel = _textLabel; |
| 60 |
| 61 - (instancetype)initWithFrame:(CGRect)frame { |
| 62 self = [super initWithFrame:frame]; |
| 63 if (self) { |
| 64 UIView* contentView = self.contentView; |
| 65 |
| 66 _textLabel = [[UILabel alloc] init]; |
| 67 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 68 [contentView addSubview:_textLabel]; |
| 69 |
| 70 _textLabel.font = |
| 71 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14]; |
| 72 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 73 _textLabel.numberOfLines = 0; |
| 74 |
| 75 // Set up the constraints. |
| 76 [NSLayoutConstraint activateConstraints:@[ |
| 77 [_textLabel.leadingAnchor |
| 78 constraintEqualToAnchor:contentView.leadingAnchor |
| 79 constant:kHorizontalPadding], |
| 80 [_textLabel.trailingAnchor |
| 81 constraintEqualToAnchor:contentView.trailingAnchor |
| 82 constant:-kHorizontalPadding], |
| 83 [_textLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor |
| 84 constant:kVerticalPadding], |
| 85 [_textLabel.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor |
| 86 constant:-kVerticalPadding], |
| 87 ]]; |
| 88 } |
| 89 return self; |
| 90 } |
| 91 |
| 92 - (void)prepareForReuse { |
| 93 [super prepareForReuse]; |
| 94 self.textLabel.text = nil; |
| 95 self.accessibilityLabel = nil; |
| 96 } |
| 97 |
| 98 // Implements -layoutSubviews as per instructions in documentation for |
| 99 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 100 - (void)layoutSubviews { |
| 101 [super layoutSubviews]; |
| 102 |
| 103 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 104 // changes, for instance on screen rotation. |
| 105 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 106 self.textLabel.preferredMaxLayoutWidth = parentWidth - 2 * kHorizontalPadding; |
| 107 |
| 108 // Re-layout with the new preferred width to allow the label to adjust its |
| 109 // height. |
| 110 [super layoutSubviews]; |
| 111 } |
| 112 |
| 113 @end |
OLD | NEW |