Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Unified Diff: ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm

Issue 2764783006: Create CollectionViewTextCell (Closed)
Patch Set: Fix tests Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..4b28223e83e774625332db10e950f00693b92c62
--- /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 preferedMaxLayoutWidth =
+ CGRectGetWidth(self.contentView.frame) - 2 * kMargin;
+ _textLabel.preferredMaxLayoutWidth = preferedMaxLayoutWidth;
+ _detailTextLabel.preferredMaxLayoutWidth = preferedMaxLayoutWidth;
+
+ // Re-layout with the new preferred width to allow the label to adjust its
+ // height.
+ [super layoutSubviews];
+}
+
+- (void)prepareForReuse {
+ self.textLabel.text = nil;
+ self.detailTextLabel.text = nil;
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698