| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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/collection_view/cells/collection_view_text_cell.h
" |
| 6 |
| 7 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 8 #error "This file requires ARC support." |
| 9 #endif |
| 10 |
| 11 namespace { |
| 12 const CGFloat kMargin = 16; |
| 13 const CGFloat kMinimalHeight = 48; |
| 14 } |
| 15 |
| 16 @implementation CollectionViewTextCell |
| 17 |
| 18 @synthesize textLabel = _textLabel; |
| 19 @synthesize detailTextLabel = _detailTextLabel; |
| 20 |
| 21 - (instancetype)initWithFrame:(CGRect)frame { |
| 22 self = [super initWithFrame:frame]; |
| 23 if (self) { |
| 24 UIView* containerView = [[UIView alloc] initWithFrame:CGRectZero]; |
| 25 containerView.translatesAutoresizingMaskIntoConstraints = NO; |
| 26 [self.contentView addSubview:containerView]; |
| 27 |
| 28 _textLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 29 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 30 [containerView addSubview:_textLabel]; |
| 31 |
| 32 _detailTextLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 33 _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 34 [containerView addSubview:_detailTextLabel]; |
| 35 |
| 36 [NSLayoutConstraint activateConstraints:@[ |
| 37 // Total height. |
| 38 // The MDC specs ask for at least 48 pt. |
| 39 [self.contentView.heightAnchor |
| 40 constraintGreaterThanOrEqualToConstant:kMinimalHeight], |
| 41 |
| 42 // Container. |
| 43 [containerView.leadingAnchor |
| 44 constraintEqualToAnchor:self.contentView.leadingAnchor |
| 45 constant:kMargin], |
| 46 [containerView.trailingAnchor |
| 47 constraintEqualToAnchor:self.contentView.trailingAnchor |
| 48 constant:-kMargin], |
| 49 [containerView.topAnchor |
| 50 constraintGreaterThanOrEqualToAnchor:self.contentView.topAnchor |
| 51 constant:kMargin], |
| 52 [containerView.bottomAnchor |
| 53 constraintLessThanOrEqualToAnchor:self.contentView.bottomAnchor |
| 54 constant:-kMargin], |
| 55 [containerView.centerYAnchor |
| 56 constraintEqualToAnchor:self.contentView.centerYAnchor], |
| 57 |
| 58 // Labels. |
| 59 [_textLabel.leadingAnchor |
| 60 constraintEqualToAnchor:containerView.leadingAnchor], |
| 61 [_textLabel.trailingAnchor |
| 62 constraintLessThanOrEqualToAnchor:containerView.trailingAnchor], |
| 63 [_textLabel.topAnchor constraintEqualToAnchor:containerView.topAnchor], |
| 64 [_textLabel.bottomAnchor |
| 65 constraintEqualToAnchor:_detailTextLabel.topAnchor], |
| 66 [_detailTextLabel.leadingAnchor |
| 67 constraintEqualToAnchor:_textLabel.leadingAnchor], |
| 68 [_detailTextLabel.trailingAnchor |
| 69 constraintLessThanOrEqualToAnchor:containerView.trailingAnchor], |
| 70 [_detailTextLabel.bottomAnchor |
| 71 constraintLessThanOrEqualToAnchor:containerView.bottomAnchor], |
| 72 ]]; |
| 73 } |
| 74 return self; |
| 75 } |
| 76 |
| 77 // Implement -layoutSubviews as per instructions in documentation for |
| 78 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 79 - (void)layoutSubviews { |
| 80 [super layoutSubviews]; |
| 81 // Adjust the text and detailText label preferredMaxLayoutWidth when the |
| 82 // parent's width changes, for instance on screen rotation. |
| 83 CGFloat preferedMaxLayoutWidth = |
| 84 CGRectGetWidth(self.contentView.frame) - 2 * kMargin; |
| 85 _textLabel.preferredMaxLayoutWidth = preferedMaxLayoutWidth; |
| 86 _detailTextLabel.preferredMaxLayoutWidth = preferedMaxLayoutWidth; |
| 87 |
| 88 // Re-layout with the new preferred width to allow the label to adjust its |
| 89 // height. |
| 90 [super layoutSubviews]; |
| 91 } |
| 92 |
| 93 - (void)prepareForReuse { |
| 94 self.textLabel.text = nil; |
| 95 self.detailTextLabel.text = nil; |
| 96 } |
| 97 |
| 98 @end |
| OLD | NEW |