OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/suggestions/suggestions_article_item.h" |
| 6 |
| 7 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." |
| 11 #endif |
| 12 |
| 13 namespace { |
| 14 const CGFloat kImageSize = 100; |
| 15 } |
| 16 |
| 17 @implementation SuggestionsArticleItem { |
| 18 NSString* _title; |
| 19 NSString* _subtitle; |
| 20 UIImage* _image; |
| 21 } |
| 22 |
| 23 - (instancetype)initWithType:(NSInteger)type |
| 24 title:(NSString*)title |
| 25 subtitle:(NSString*)subtitle |
| 26 image:(UIImage*)image { |
| 27 self = [super initWithType:type]; |
| 28 if (self) { |
| 29 self.cellClass = [SuggestionsArticleCell class]; |
| 30 _title = title; |
| 31 _subtitle = subtitle; |
| 32 _image = image; |
| 33 } |
| 34 return self; |
| 35 } |
| 36 |
| 37 - (void)configureCell:(SuggestionsArticleCell*)cell { |
| 38 [super configureCell:cell]; |
| 39 cell.title = _title; |
| 40 cell.subtitle = _subtitle; |
| 41 cell.image = _image; |
| 42 } |
| 43 |
| 44 @end |
| 45 |
| 46 @interface SuggestionsArticleCell () { |
| 47 UIImageView* _imageView; |
| 48 UILabel* _titleLabel; |
| 49 UILabel* _subtitleLabel; |
| 50 UIView* _imageContainer; |
| 51 } |
| 52 |
| 53 @end |
| 54 |
| 55 @implementation SuggestionsArticleCell |
| 56 |
| 57 @synthesize title = _title; |
| 58 @synthesize subtitle = _subtitle; |
| 59 @synthesize image = _image; |
| 60 |
| 61 - (instancetype)initWithFrame:(CGRect)frame { |
| 62 self = [super initWithFrame:frame]; |
| 63 if (self) { |
| 64 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 65 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
| 66 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; |
| 67 _imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; |
| 68 |
| 69 _subtitleLabel.numberOfLines = 0; |
| 70 |
| 71 _imageContainer.translatesAutoresizingMaskIntoConstraints = NO; |
| 72 _imageView.translatesAutoresizingMaskIntoConstraints = NO; |
| 73 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 74 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; |
| 75 |
| 76 [_imageContainer addSubview:_imageView]; |
| 77 [self.contentView addSubview:_imageContainer]; |
| 78 [self.contentView addSubview:_titleLabel]; |
| 79 [self.contentView addSubview:_subtitleLabel]; |
| 80 |
| 81 ApplyVisualConstraintsWithMetrics( |
| 82 @[ |
| 83 @"H:|-[title]-[imageContainer(imageSize)]-|", |
| 84 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", |
| 85 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", |
| 86 @"V:|-[imageContainer(>=imageSize)]-|" |
| 87 ], |
| 88 @{ |
| 89 @"image" : _imageView, |
| 90 @"imageContainer" : _imageContainer, |
| 91 @"title" : _titleLabel, |
| 92 @"text" : _subtitleLabel |
| 93 }, |
| 94 @{ @"imageSize" : @(kImageSize) }); |
| 95 } |
| 96 return self; |
| 97 } |
| 98 |
| 99 #pragma mark - Properties |
| 100 |
| 101 - (void)setTitle:(NSString*)title { |
| 102 _titleLabel.text = title; |
| 103 _title = title; |
| 104 } |
| 105 |
| 106 - (void)setSubtitle:(NSString*)subtitle { |
| 107 _subtitleLabel.text = subtitle; |
| 108 _subtitle = subtitle; |
| 109 } |
| 110 |
| 111 - (void)setImage:(UIImage*)image { |
| 112 _imageView.image = image; |
| 113 _image = image; |
| 114 } |
| 115 |
| 116 #pragma mark - UIView |
| 117 |
| 118 // Implements -layoutSubviews as per instructions in documentation for |
| 119 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
| 120 - (void)layoutSubviews { |
| 121 [super layoutSubviews]; |
| 122 |
| 123 // Adjust the text label preferredMaxLayoutWidth when the parent's width |
| 124 // changes, for instance on screen rotation. |
| 125 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); |
| 126 _subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; |
| 127 |
| 128 // Re-layout with the new preferred width to allow the label to adjust its |
| 129 // height. |
| 130 [super layoutSubviews]; |
| 131 } |
| 132 |
| 133 @end |
OLD | NEW |