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

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

Issue 2764783006: Create CollectionViewTextCell (Closed)
Patch Set: Add comment 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..77f0dce2e3dea4fd73d5c6b2268537c2016fbbdb
--- /dev/null
+++ b/ios/chrome/browser/ui/collection_view/cells/collection_view_text_cell.mm
@@ -0,0 +1,92 @@
+// 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.
+ [self.contentView.heightAnchor
+ constraintGreaterThanOrEqualToConstant:kMinimalHeight],
lpromero 2017/03/22 15:51:47 Why? Material spec?
gambard 2017/03/23 09:41:29 Yes.
+
+ // Container.
+ [containerView.leadingAnchor
lpromero 2017/03/22 15:51:47 Why do you need the container? I'd remove it.
gambard 2017/03/23 09:41:29 The container is needed to center the labels in ca
+ 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],
lpromero 2017/03/22 15:51:47 You already set top and bottom, why do you need th
gambard 2017/03/23 09:41:29 Same as above.
lpromero 2017/03/23 12:23:35 I see. And I didn't notice that the top/bottom whe
+
+ // Labels.
+ [_textLabel.leadingAnchor
+ constraintEqualToAnchor:containerView.leadingAnchor],
+ [_textLabel.trailingAnchor
+ constraintLessThanOrEqualToAnchor:containerView.trailingAnchor],
lpromero 2017/03/22 15:51:47 Why not going all the way to the trailing edge?
gambard 2017/03/23 09:41:29 We had some problem with RTL + editing when the la
+ [_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 =
lpromero 2017/03/23 12:23:35 preferred
gambard 2017/03/23 15:32:28 Done.
+ 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];
+}
lpromero 2017/03/22 15:51:47 Can you add prepareForReuse?
gambard 2017/03/23 09:41:29 Done. I am not sure about what to reset in prepare
lpromero 2017/03/23 12:23:35 I guess we'll have to do incrementally.
gambard 2017/03/23 15:32:28 Acknowledged.
+
lpromero 2017/03/22 15:51:47 Can you add proper accessibility? I think it's jus
gambard 2017/03/23 09:41:29 The accessibility is done in the item.
lpromero 2017/03/23 12:23:35 Acknowledged.
+@end

Powered by Google App Engine
This is Rietveld 408576698