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 @interface SuggestionsArticleItem () | |
18 | |
19 @property(nonatomic, copy) NSString* title; | |
20 @property(nonatomic, copy) NSString* subtitle; | |
21 @property(nonatomic, strong) UIImage* image; | |
22 | |
23 @end | |
24 | |
25 @implementation SuggestionsArticleItem | |
26 | |
27 @synthesize title = _title; | |
28 @synthesize subtitle = _subtitle; | |
29 @synthesize image = _image; | |
30 | |
31 - (instancetype)initWithType:(NSInteger)type | |
32 title:(NSString*)title | |
33 subtitle:(NSString*)subtitle | |
34 image:(UIImage*)image { | |
35 self = [super initWithType:type]; | |
36 if (self) { | |
37 self.cellClass = [SuggestionsArticleCell class]; | |
38 _title = [title copy]; | |
39 _subtitle = [subtitle copy]; | |
40 _image = image; | |
41 } | |
42 return self; | |
43 } | |
44 | |
45 - (void)configureCell:(SuggestionsArticleCell*)cell { | |
46 [super configureCell:cell]; | |
47 cell.titleLabel.text = _title; | |
48 cell.subtitleLabel.text = _subtitle; | |
49 cell.imageView.image = _image; | |
50 } | |
51 | |
52 @end | |
53 | |
54 @implementation SuggestionsArticleCell | |
55 | |
56 @synthesize titleLabel = _titleLabel; | |
57 @synthesize subtitleLabel = _subtitleLabel; | |
58 @synthesize imageView = _imageView; | |
59 | |
60 - (instancetype)initWithFrame:(CGRect)frame { | |
61 self = [super initWithFrame:frame]; | |
62 if (self) { | |
63 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
64 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
65 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
66 UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
67 | |
68 _subtitleLabel.numberOfLines = 0; | |
69 | |
70 imageContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
71 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
72 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
73 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
74 | |
75 [imageContainer addSubview:_imageView]; | |
76 [self.contentView addSubview:imageContainer]; | |
77 [self.contentView addSubview:_titleLabel]; | |
78 [self.contentView addSubview:_subtitleLabel]; | |
79 | |
80 ApplyVisualConstraintsWithMetrics( | |
81 @[ | |
82 @"H:|-[title]-[imageContainer(imageSize)]-|", | |
83 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", | |
84 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", | |
85 @"V:|-[imageContainer(>=imageSize)]-|" | |
86 ], | |
87 @{ | |
88 @"image" : _imageView, | |
89 @"imageContainer" : imageContainer, | |
90 @"title" : _titleLabel, | |
91 @"text" : _subtitleLabel | |
92 }, | |
93 @{ @"imageSize" : @(kImageSize) }); | |
94 } | |
95 return self; | |
96 } | |
97 | |
98 #pragma mark - UIView | |
99 | |
100 // Implements -layoutSubviews as per instructions in documentation for | |
101 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
102 - (void)layoutSubviews { | |
103 [super layoutSubviews]; | |
104 | |
105 // Adjust the text label preferredMaxLayoutWidth when the parent's width | |
106 // changes, for instance on screen rotation. | |
107 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | |
108 self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | |
109 | |
110 // Re-layout with the new preferred width to allow the label to adjust its | |
111 // height. | |
112 [super layoutSubviews]; | |
113 } | |
114 | |
115 @end | |
OLD | NEW |