Chromium Code Reviews| Index: ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm |
| diff --git a/ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm b/ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b156c02efc73038d3fe5bc88f760f651ee99f665 |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +const CGFloat kMargin = 16; |
| +const CGFloat kMinimalHeight = 48; |
| +} |
| + |
| +@implementation CollectionViewTextCell |
| + |
| +@synthesize textLabel = _textLabel; |
| +@synthesize detailTextLabel = _detailTextLabel; |
| + |
| +- (instancetype)initWithFrame:(CGRect)frame { |
| + self = [super initWithFrame:frame]; |
| + if (self) { |
| + UIView* containerView = [[UIView alloc] initWithFrame:CGRectZero]; |
| + containerView.translatesAutoresizingMaskIntoConstraints = NO; |
| + [self.contentView addSubview:containerView]; |
| + |
| + _textLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| + _textLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| + [containerView addSubview:_textLabel]; |
| + |
| + _detailTextLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| + _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| + [containerView addSubview:_detailTextLabel]; |
| + |
| + [NSLayoutConstraint activateConstraints:@[ |
| + // Total height. |
| + // The MDC specs ask for at least 48 pt. |
| + [self.contentView.heightAnchor |
| + constraintGreaterThanOrEqualToConstant:kMinimalHeight], |
| + |
| + // Container. |
| + [containerView.leadingAnchor |
| + constraintEqualToAnchor:self.contentView.leadingAnchor |
| + constant:kMargin], |
| + [containerView.trailingAnchor |
| + constraintEqualToAnchor:self.contentView.trailingAnchor |
| + constant:-kMargin], |
| + [containerView.topAnchor |
| + constraintGreaterThanOrEqualToAnchor:self.contentView.topAnchor |
| + constant:kMargin], |
| + [containerView.bottomAnchor |
| + constraintLessThanOrEqualToAnchor:self.contentView.bottomAnchor |
| + constant:-kMargin], |
| + [containerView.centerYAnchor |
| + constraintEqualToAnchor:self.contentView.centerYAnchor], |
| + |
| + // Labels. |
| + [_textLabel.leadingAnchor |
| + constraintEqualToAnchor:containerView.leadingAnchor], |
| + [_textLabel.trailingAnchor |
| + constraintLessThanOrEqualToAnchor:containerView.trailingAnchor], |
| + [_textLabel.topAnchor constraintEqualToAnchor:containerView.topAnchor], |
| + [_textLabel.bottomAnchor |
| + constraintEqualToAnchor:_detailTextLabel.topAnchor], |
| + [_detailTextLabel.leadingAnchor |
| + constraintEqualToAnchor:_textLabel.leadingAnchor], |
| + [_detailTextLabel.trailingAnchor |
| + constraintLessThanOrEqualToAnchor:containerView.trailingAnchor], |
| + [_detailTextLabel.bottomAnchor |
| + constraintLessThanOrEqualToAnchor:containerView.bottomAnchor], |
| + ]]; |
| + } |
| + return self; |
| +} |
| + |
| +// Implement -layoutSubviews as per instructions in documentation for |
| +// +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| +- (void)layoutSubviews { |
| + [super layoutSubviews]; |
| + // Adjust the text and detailText label preferredMaxLayoutWidth when the |
| + // parent's width changes, for instance on screen rotation. |
| + CGFloat prefferedMaxLayoutWidth = |
| + CGRectGetWidth(self.contentView.frame) - 2 * kMargin; |
| + _textLabel.preferredMaxLayoutWidth = prefferedMaxLayoutWidth; |
| + _detailTextLabel.preferredMaxLayoutWidth = prefferedMaxLayoutWidth; |
| + |
| + // Re-layout with the new preferred width to allow the label to adjust its |
| + // height. |
| + [super layoutSubviews]; |
| +} |
| + |
| +- (void)prepareForReuse { |
| + self.textLabel.text = @""; |
|
lpromero
2017/03/23 12:23:35
Use nil
gambard
2017/03/23 15:32:28
Done.
|
| + self.detailTextLabel.text = @""; |
|
lpromero
2017/03/23 12:23:35
My rule of thumb is to reset everything set in con
gambard
2017/03/23 15:32:28
Acknowledged.
|
| +} |
| + |
| +@end |