Chromium Code Reviews| 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..d7910031d04deb26a982cabc128456ffd81ba29a |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/suggestions/suggestions_article_item.mm |
| @@ -0,0 +1,121 @@ |
| +// 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; |
| +} |
| + |
| +@interface SuggestionsArticleItem () |
| + |
| +@property(nonatomic, copy) NSString* title; |
| +@property(nonatomic, copy) NSString* subtitle; |
| +@property(nonatomic, strong) UIImage* image; |
| + |
| +@end |
| + |
| +@implementation SuggestionsArticleItem |
| + |
| +@synthesize title = _title; |
| +@synthesize subtitle = _subtitle; |
| +@synthesize image = _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 copy]; |
| + _subtitle = [subtitle copy]; |
| + _image = image; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)configureCell:(SuggestionsArticleCell*)cell { |
| + [super configureCell:cell]; |
| + cell.titleLabel.text = _title; |
| + cell.subtitleLabel.text = _subtitle; |
| + cell.imageView.image = _image; |
| +} |
| + |
| +@end |
| + |
| +@interface SuggestionsArticleCell () { |
| + UIView* _imageContainer; |
|
lpromero
2017/01/12 09:57:21
Nit: you only need the container in the init. Cons
gambard
2017/01/12 12:55:12
Done.
|
| +} |
| + |
| +@end |
| + |
| +@implementation SuggestionsArticleCell |
| + |
| +@synthesize titleLabel = _titleLabel; |
| +@synthesize subtitleLabel = _subtitleLabel; |
| +@synthesize imageView = _imageView; |
| + |
| +- (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 - 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); |
| + self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; |
| + |
| + // Re-layout with the new preferred width to allow the label to adjust its |
| + // height. |
| + [super layoutSubviews]; |
| +} |
| + |
| +@end |