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 @interface SuggestionsArticleCell () { | |
55 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.
| |
56 } | |
57 | |
58 @end | |
59 | |
60 @implementation SuggestionsArticleCell | |
61 | |
62 @synthesize titleLabel = _titleLabel; | |
63 @synthesize subtitleLabel = _subtitleLabel; | |
64 @synthesize imageView = _imageView; | |
65 | |
66 - (instancetype)initWithFrame:(CGRect)frame { | |
67 self = [super initWithFrame:frame]; | |
68 if (self) { | |
69 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
70 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
71 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
72 _imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
73 | |
74 _subtitleLabel.numberOfLines = 0; | |
75 | |
76 _imageContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
77 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
78 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
79 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
80 | |
81 [_imageContainer addSubview:_imageView]; | |
82 [self.contentView addSubview:_imageContainer]; | |
83 [self.contentView addSubview:_titleLabel]; | |
84 [self.contentView addSubview:_subtitleLabel]; | |
85 | |
86 ApplyVisualConstraintsWithMetrics( | |
87 @[ | |
88 @"H:|-[title]-[imageContainer(imageSize)]-|", | |
89 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", | |
90 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", | |
91 @"V:|-[imageContainer(>=imageSize)]-|" | |
92 ], | |
93 @{ | |
94 @"image" : _imageView, | |
95 @"imageContainer" : _imageContainer, | |
96 @"title" : _titleLabel, | |
97 @"text" : _subtitleLabel | |
98 }, | |
99 @{ @"imageSize" : @(kImageSize) }); | |
100 } | |
101 return self; | |
102 } | |
103 | |
104 #pragma mark - UIView | |
105 | |
106 // Implements -layoutSubviews as per instructions in documentation for | |
107 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
108 - (void)layoutSubviews { | |
109 [super layoutSubviews]; | |
110 | |
111 // Adjust the text label preferredMaxLayoutWidth when the parent's width | |
112 // changes, for instance on screen rotation. | |
113 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | |
114 self.subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | |
115 | |
116 // Re-layout with the new preferred width to allow the label to adjust its | |
117 // height. | |
118 [super layoutSubviews]; | |
119 } | |
120 | |
121 @end | |
OLD | NEW |