Index: ios/chrome/browser/ui/suggestions/suggestions_article_item.mm |
diff --git a/ios/chrome/browser/ui/suggestions/suggestions_article_item.mm b/ios/chrome/browser/ui/suggestions/suggestions_article_item.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..eefb74cf68554f908407ded8b2c3ab26f5915754 |
--- /dev/null |
+++ b/ios/chrome/browser/ui/suggestions/suggestions_article_item.mm |
@@ -0,0 +1,133 @@ |
+// 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/suggestions/suggestions_article_item.h" |
+ |
+#import "ios/chrome/browser/ui/uikit_ui_util.h" |
+ |
+#if !defined(__has_feature) || !__has_feature(objc_arc) |
+#error "This file requires ARC support." |
+#endif |
+ |
+namespace { |
+const CGFloat kImageSize = 100; |
+} |
+ |
+@implementation SuggestionsArticleItem { |
+ NSString* _title; |
+ NSString* _subtitle; |
+ UIImage* _image; |
+} |
+ |
+- (instancetype)initWithType:(NSInteger)type |
+ title:(NSString*)title |
+ subtitle:(NSString*)subtitle |
+ image:(UIImage*)image { |
+ self = [super initWithType:type]; |
+ if (self) { |
+ self.cellClass = [SuggestionsArticleCell class]; |
+ _title = title; |
+ _subtitle = subtitle; |
+ _image = image; |
+ } |
+ return self; |
+} |
+ |
+- (void)configureCell:(SuggestionsArticleCell*)cell { |
+ [super configureCell:cell]; |
+ cell.title = _title; |
+ cell.subtitle = _subtitle; |
+ cell.image = _image; |
+} |
+ |
+@end |
+ |
+@interface SuggestionsArticleCell () { |
+ UIImageView* _imageView; |
+ UILabel* _titleLabel; |
+ UILabel* _subtitleLabel; |
+ UIView* _imageContainer; |
+} |
+ |
+@end |
+ |
+@implementation SuggestionsArticleCell |
+ |
+@synthesize title = _title; |
+@synthesize subtitle = _subtitle; |
+@synthesize image = _image; |
+ |
+- (instancetype)initWithFrame:(CGRect)frame { |
+ self = [super initWithFrame:frame]; |
+ if (self) { |
+ _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
+ _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
+ _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
+ _imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; |
+ |
+ _subtitleLabel.numberOfLines = 0; |
+ |
+ _imageContainer.translatesAutoresizingMaskIntoConstraints = NO; |
+ _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
+ _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
+ _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
+ |
+ [_imageContainer addSubview:_imageView]; |
+ [self.contentView addSubview:_imageContainer]; |
+ [self.contentView addSubview:_titleLabel]; |
+ [self.contentView addSubview:_subtitleLabel]; |
+ |
+ ApplyVisualConstraintsWithMetrics( |
+ @[ |
+ @"H:|-[title]-[imageContainer(imageSize)]-|", |
+ @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", |
+ @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", |
+ @"V:|-[imageContainer(>=imageSize)]-|" |
+ ], |
+ @{ |
+ @"image" : _imageView, |
+ @"imageContainer" : _imageContainer, |
+ @"title" : _titleLabel, |
+ @"text" : _subtitleLabel |
+ }, |
+ @{ @"imageSize" : @(kImageSize) }); |
+ } |
+ return self; |
+} |
+ |
+#pragma mark - Properties |
+ |
+- (void)setTitle:(NSString*)title { |
+ _titleLabel.text = title; |
+ _title = title; |
+} |
+ |
+- (void)setSubtitle:(NSString*)subtitle { |
+ _subtitleLabel.text = subtitle; |
+ _subtitle = subtitle; |
+} |
+ |
+- (void)setImage:(UIImage*)image { |
+ _imageView.image = image; |
+ _image = image; |
+} |
+ |
+#pragma mark - UIView |
+ |
+// Implements -layoutSubviews as per instructions in documentation for |
+// +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
+- (void)layoutSubviews { |
+ [super layoutSubviews]; |
+ |
+ // Adjust the text label preferredMaxLayoutWidth when the parent's width |
+ // changes, for instance on screen rotation. |
+ CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
+ _subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; |
+ |
+ // Re-layout with the new preferred width to allow the label to adjust its |
+ // height. |
+ [super layoutSubviews]; |
+} |
+ |
+@end |