Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item.mm

Issue 2588713002: Upstream Chrome on iOS source code [4/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/collection_view/cells/collection_view_footer_item .h"
6
7 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
8 #import "ios/chrome/browser/ui/util/label_link_controller.h"
9 #import "ios/chrome/common/string_util.h"
10 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat erialPalettes.h"
11 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
12
13 #if !defined(__has_feature) || !__has_feature(objc_arc)
14 #error "This file requires ARC support."
15 #endif
16
17 namespace {
18 // Padding used on the leading and trailing edges of the cell.
19 const CGFloat kDefaultHorizontalPadding = 24;
20
21 // Padding used on the leading edge for the text when the cell has an image.
22 const CGFloat kImageRightMargin = 10;
23
24 // Padding used on the top and bottom edges of the cell.
25 const CGFloat kVerticalPadding = 16;
26 } // namespace
27
28 @interface CollectionViewFooterCell ()
29
30 // Delegate to notify when the link in |textLabel| is tapped.
31 @property(nonatomic, weak) id<CollectionViewFooterLinkDelegate> linkDelegate;
32
33 // Sets the URL to load when the link in |textLabel| is tapped.
34 - (void)setLabelLinkURL:(const GURL&)URL;
35
36 @end
37
38 @implementation CollectionViewFooterItem
39
40 @synthesize text = _text;
41 @synthesize linkURL = _linkURL;
42 @synthesize linkDelegate = _linkDelegate;
43 @synthesize image = _image;
44
45 - (instancetype)initWithType:(NSInteger)type {
46 self = [super initWithType:type];
47 if (self) {
48 self.cellClass = [CollectionViewFooterCell class];
49 }
50 return self;
51 }
52
53 #pragma mark CollectionViewItem
54
55 - (void)configureCell:(CollectionViewFooterCell*)cell {
56 [super configureCell:cell];
57 cell.textLabel.text = _text;
58 [cell setLabelLinkURL:_linkURL];
59 cell.linkDelegate = _linkDelegate;
60 cell.imageView.image = _image;
61 }
62
63 @end
64
65 @interface CollectionViewFooterCell () {
66 LabelLinkController* _linkController;
67 NSLayoutConstraint* _textLeadingAnchorConstraint;
68 NSLayoutConstraint* _imageLeadingAnchorConstraint;
69 }
70 @end
71
72 @implementation CollectionViewFooterCell
73
74 @synthesize textLabel = _textLabel;
75 @synthesize imageView = _imageView;
76 @synthesize linkDelegate = _linkDelegate;
77 @synthesize horizontalPadding = _horizontalPadding;
78
79 - (instancetype)initWithFrame:(CGRect)frame {
80 self = [super initWithFrame:frame];
81 if (self) {
82 self.isAccessibilityElement = YES;
83
84 _textLabel = [[UILabel alloc] init];
85 _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
86 _textLabel.backgroundColor = [UIColor clearColor];
87 [self.contentView addSubview:_textLabel];
88
89 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
90 _imageView.translatesAutoresizingMaskIntoConstraints = NO;
91 [self.contentView addSubview:_imageView];
92
93 _textLabel.font =
94 [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
95 _textLabel.textColor = [[MDCPalette greyPalette] tint900];
96 _textLabel.shadowOffset = CGSizeMake(1.f, 0.f);
97 _textLabel.shadowColor = [UIColor whiteColor];
98 _textLabel.numberOfLines = 0;
99 _textLabel.lineBreakMode = NSLineBreakByWordWrapping;
100
101 _horizontalPadding = kDefaultHorizontalPadding;
102 _textLeadingAnchorConstraint = [_textLabel.leadingAnchor
103 constraintEqualToAnchor:_imageView.trailingAnchor];
104 _imageLeadingAnchorConstraint = [_imageView.leadingAnchor
105 constraintEqualToAnchor:self.contentView.leadingAnchor
106 constant:_horizontalPadding];
107 [NSLayoutConstraint activateConstraints:@[
108 [_textLabel.topAnchor constraintEqualToAnchor:self.contentView.topAnchor
109 constant:kVerticalPadding],
110 [_textLabel.bottomAnchor
111 constraintEqualToAnchor:self.contentView.bottomAnchor
112 constant:-kVerticalPadding],
113 [_imageView.centerYAnchor
114 constraintEqualToAnchor:self.contentView.centerYAnchor],
115 _textLeadingAnchorConstraint,
116 _imageLeadingAnchorConstraint,
117 ]];
118 }
119 return self;
120 }
121
122 - (void)setLabelLinkURL:(const GURL&)URL {
123 _linkController = nil;
124 if (!URL.is_valid()) {
125 return;
126 }
127
128 NSRange range;
129 NSString* text = _textLabel.text;
130 _textLabel.text = ParseStringWithLink(text, &range);
131 if (range.location != NSNotFound && range.length != 0) {
132 __weak CollectionViewFooterCell* weakSelf = self;
133 _linkController = [[LabelLinkController alloc]
134 initWithLabel:_textLabel
135 action:^(const GURL& URL) {
136 [weakSelf.linkDelegate cell:weakSelf didTapLinkURL:URL];
137 }];
138 [_linkController setLinkColor:[[MDCPalette cr_bluePalette] tint500]];
139 [_linkController addLinkWithRange:range url:URL];
140 }
141 }
142
143 - (void)layoutSubviews {
144 [super layoutSubviews];
145
146 _imageLeadingAnchorConstraint.constant = _horizontalPadding;
147
148 // Adjust the text label preferredMaxLayoutWidth when the parent's width
149 // changes, for instance on screen rotation.
150 CGFloat parentWidth = self.contentView.frame.size.width;
151 if (_imageView.image) {
152 _textLabel.preferredMaxLayoutWidth =
153 parentWidth - 2.f * _imageLeadingAnchorConstraint.constant -
154 kImageRightMargin - _imageView.image.size.width;
155 _textLeadingAnchorConstraint.constant = kImageRightMargin;
156 } else {
157 _textLabel.preferredMaxLayoutWidth =
158 parentWidth - 2.f * _imageLeadingAnchorConstraint.constant;
159 _textLeadingAnchorConstraint.constant = 0;
160 }
161
162 // Re-layout with the new preferred width to allow the label to adjust its
163 // height.
164 [super layoutSubviews];
165 }
166
167 - (void)prepareForReuse {
168 [super prepareForReuse];
169 self.textLabel.text = nil;
170 self.imageView.image = nil;
171 [self setLabelLinkURL:GURL()];
172 self.horizontalPadding = kDefaultHorizontalPadding;
173 _linkController = nil;
174 _linkDelegate = nil;
175 }
176
177 #pragma mark - Accessibility
178
179 - (BOOL)isAccessibilityElement {
180 return NO; // Accessibility for this element is handled in
181 // LabelLinkController's TransparentLinkButton objects.
182 }
183
184 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698