OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/ntp/whats_new_header_view.h" | 5 #import "ios/chrome/browser/ui/ntp/whats_new_header_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/scoped_nsobject.h" | |
9 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 8 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
10 #include "ios/chrome/common/string_util.h" | 9 #include "ios/chrome/common/string_util.h" |
11 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 10 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
12 #include "ios/public/provider/chrome/browser/images/branded_image_provider.h" | 11 #include "ios/public/provider/chrome/browser/images/branded_image_provider.h" |
13 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | 12 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
14 | 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
15 namespace { | 18 namespace { |
16 | 19 |
17 const CGFloat kLabelTopMargin = 16; | 20 const CGFloat kLabelTopMargin = 16; |
18 const CGFloat kLabelBottomMargin = 24; | 21 const CGFloat kLabelBottomMargin = 24; |
19 const CGFloat kLabelLineSpacing = 4; | 22 const CGFloat kLabelLineSpacing = 4; |
20 const CGFloat kLabelLeftMargin = 8; | 23 const CGFloat kLabelLeftMargin = 8; |
21 const CGFloat kLabelFontSize = 14; | 24 const CGFloat kLabelFontSize = 14; |
22 const CGFloat kInfoIconSize = 24; | 25 const CGFloat kInfoIconSize = 24; |
23 const CGFloat kInfoIconTopMargin = 12; | 26 const CGFloat kInfoIconTopMargin = 12; |
24 | 27 |
25 const int kTextColorRgb = 0x333333; | 28 const int kTextColorRgb = 0x333333; |
26 const int kLinkColorRgb = 0x5595FE; | 29 const int kLinkColorRgb = 0x5595FE; |
27 | 30 |
28 } // namespace | 31 } // namespace |
29 | 32 |
30 @interface WhatsNewHeaderView () { | 33 @interface WhatsNewHeaderView () { |
31 base::scoped_nsobject<UIImageView> _infoIconImageView; | 34 UIImageView* _infoIconImageView; |
32 base::scoped_nsobject<UILabel> _promoLabel; | 35 UILabel* _promoLabel; |
33 base::scoped_nsobject<NSLayoutConstraint> _edgeConstraint; | 36 NSLayoutConstraint* _edgeConstraint; |
34 base::scoped_nsobject<UIView> _leftSpacer; | 37 UIView* _leftSpacer; |
35 base::scoped_nsobject<UIView> _rightSpacer; | 38 UIView* _rightSpacer; |
36 CGFloat _sideMargin; | 39 CGFloat _sideMargin; |
37 } | 40 } |
38 | 41 |
39 @end | 42 @end |
40 | 43 |
41 @implementation WhatsNewHeaderView | 44 @implementation WhatsNewHeaderView |
42 | 45 |
43 @synthesize delegate = _delegate; | 46 @synthesize delegate = _delegate; |
44 | 47 |
45 - (instancetype)initWithFrame:(CGRect)frame { | 48 - (instancetype)initWithFrame:(CGRect)frame { |
46 self = [super initWithFrame:frame]; | 49 self = [super initWithFrame:frame]; |
47 if (self) { | 50 if (self) { |
48 self.hidden = YES; | 51 self.hidden = YES; |
49 UIImage* infoIconImage = ios::GetChromeBrowserProvider() | 52 UIImage* infoIconImage = ios::GetChromeBrowserProvider() |
50 ->GetBrandedImageProvider() | 53 ->GetBrandedImageProvider() |
51 ->GetWhatsNewIconImage(WHATS_NEW_INFO); | 54 ->GetWhatsNewIconImage(WHATS_NEW_INFO); |
52 _infoIconImageView.reset([[UIImageView alloc] initWithImage:infoIconImage]); | 55 _infoIconImageView = [[UIImageView alloc] initWithImage:infoIconImage]; |
53 _promoLabel.reset([[[self class] promoLabel] retain]); | 56 _promoLabel = [[self class] promoLabel]; |
54 [_promoLabel setUserInteractionEnabled:YES]; | 57 [_promoLabel setUserInteractionEnabled:YES]; |
55 base::scoped_nsobject<UITapGestureRecognizer> promoTapRecognizer( | 58 UITapGestureRecognizer* promoTapRecognizer = [[UITapGestureRecognizer alloc] |
56 [[UITapGestureRecognizer alloc] | 59 initWithTarget:self |
57 initWithTarget:self | 60 action:@selector(promoButtonPressed)]; |
58 action:@selector(promoButtonPressed)]); | |
59 [_promoLabel addGestureRecognizer:promoTapRecognizer]; | 61 [_promoLabel addGestureRecognizer:promoTapRecognizer]; |
60 _leftSpacer.reset([[UIView alloc] initWithFrame:CGRectZero]); | 62 _leftSpacer = [[UIView alloc] initWithFrame:CGRectZero]; |
61 _rightSpacer.reset([[UIView alloc] initWithFrame:CGRectZero]); | 63 _rightSpacer = [[UIView alloc] initWithFrame:CGRectZero]; |
62 | 64 |
63 [_leftSpacer setHidden:YES]; | 65 [_leftSpacer setHidden:YES]; |
64 [_rightSpacer setHidden:YES]; | 66 [_rightSpacer setHidden:YES]; |
65 | 67 |
66 [self addSubview:_infoIconImageView]; | 68 [self addSubview:_infoIconImageView]; |
67 [self addSubview:_promoLabel]; | 69 [self addSubview:_promoLabel]; |
68 [self addSubview:_leftSpacer]; | 70 [self addSubview:_leftSpacer]; |
69 [self addSubview:_rightSpacer]; | 71 [self addSubview:_rightSpacer]; |
70 [self setTranslatesAutoresizingMaskIntoConstraints:NO]; | 72 [self setTranslatesAutoresizingMaskIntoConstraints:NO]; |
71 [_infoIconImageView setTranslatesAutoresizingMaskIntoConstraints:NO]; | 73 [_infoIconImageView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
(...skipping 26 matching lines...) Expand all Loading... |
98 @"rightSpacer" : _rightSpacer, | 100 @"rightSpacer" : _rightSpacer, |
99 }; | 101 }; |
100 | 102 |
101 ApplyVisualConstraintsWithMetrics( | 103 ApplyVisualConstraintsWithMetrics( |
102 @[ | 104 @[ |
103 verticalLogoVisualFormat, verticalLabelVisualFormat, | 105 verticalLogoVisualFormat, verticalLabelVisualFormat, |
104 horizontalVisualFormat | 106 horizontalVisualFormat |
105 ], | 107 ], |
106 views, metrics, self); | 108 views, metrics, self); |
107 | 109 |
108 _edgeConstraint.reset([[NSLayoutConstraint | 110 _edgeConstraint = [NSLayoutConstraint |
109 constraintWithItem:_leftSpacer | 111 constraintWithItem:_leftSpacer |
110 attribute:NSLayoutAttributeWidth | 112 attribute:NSLayoutAttributeWidth |
111 relatedBy:NSLayoutRelationGreaterThanOrEqual | 113 relatedBy:NSLayoutRelationGreaterThanOrEqual |
112 toItem:nil | 114 toItem:nil |
113 attribute:NSLayoutAttributeNotAnAttribute | 115 attribute:NSLayoutAttributeNotAnAttribute |
114 multiplier:1 | 116 multiplier:1 |
115 constant:_sideMargin] retain]); | 117 constant:_sideMargin]; |
116 [self addConstraint:_edgeConstraint]; | 118 [self addConstraint:_edgeConstraint]; |
117 } | 119 } |
118 [_edgeConstraint setConstant:_sideMargin]; | 120 [_edgeConstraint setConstant:_sideMargin]; |
119 [super updateConstraints]; | 121 [super updateConstraints]; |
120 } | 122 } |
121 | 123 |
122 - (void)setText:(NSString*)text { | 124 - (void)setText:(NSString*)text { |
123 [[self class] setText:text inPromoLabel:_promoLabel]; | 125 [[self class] setText:text inPromoLabel:_promoLabel]; |
124 self.hidden = NO; | 126 self.hidden = NO; |
125 } | 127 } |
126 | 128 |
127 - (void)setIcon:(WhatsNewIcon)icon { | 129 - (void)setIcon:(WhatsNewIcon)icon { |
128 UIImage* image = ios::GetChromeBrowserProvider() | 130 UIImage* image = ios::GetChromeBrowserProvider() |
129 ->GetBrandedImageProvider() | 131 ->GetBrandedImageProvider() |
130 ->GetWhatsNewIconImage(icon); | 132 ->GetWhatsNewIconImage(icon); |
131 [_infoIconImageView setImage:image]; | 133 [_infoIconImageView setImage:image]; |
132 } | 134 } |
133 | 135 |
134 - (void)setSideMargin:(CGFloat)sideMargin forWidth:(CGFloat)width { | 136 - (void)setSideMargin:(CGFloat)sideMargin forWidth:(CGFloat)width { |
135 _sideMargin = sideMargin; | 137 _sideMargin = sideMargin; |
136 [self setNeedsUpdateConstraints]; | 138 [self setNeedsUpdateConstraints]; |
137 CGFloat maxLabelWidth = | 139 CGFloat maxLabelWidth = |
138 width - 2 * sideMargin - kInfoIconSize - kLabelLeftMargin; | 140 width - 2 * sideMargin - kInfoIconSize - kLabelLeftMargin; |
139 [_promoLabel.get() setPreferredMaxLayoutWidth:maxLabelWidth]; | 141 [_promoLabel setPreferredMaxLayoutWidth:maxLabelWidth]; |
140 } | 142 } |
141 | 143 |
142 - (void)promoButtonPressed { | 144 - (void)promoButtonPressed { |
143 [_delegate onPromoLabelTapped]; | 145 [_delegate onPromoLabelTapped]; |
144 [self removeConstraints:self.constraints]; | 146 [self removeConstraints:self.constraints]; |
145 _edgeConstraint.reset(); | 147 _edgeConstraint = nil; |
146 } | 148 } |
147 | 149 |
148 + (void)setText:(NSString*)promoLabelText inPromoLabel:(UILabel*)promoLabel { | 150 + (void)setText:(NSString*)promoLabelText inPromoLabel:(UILabel*)promoLabel { |
149 DCHECK(promoLabelText); | 151 DCHECK(promoLabelText); |
150 NSRange linkRange; | 152 NSRange linkRange; |
151 NSString* strippedText = ParseStringWithLink(promoLabelText, &linkRange); | 153 NSString* strippedText = ParseStringWithLink(promoLabelText, &linkRange); |
152 DCHECK_NE(NSNotFound, static_cast<NSInteger>(linkRange.location)); | 154 DCHECK_NE(NSNotFound, static_cast<NSInteger>(linkRange.location)); |
153 DCHECK_NE(0u, linkRange.length); | 155 DCHECK_NE(0u, linkRange.length); |
154 | 156 |
155 NSMutableAttributedString* attributedText = | 157 NSMutableAttributedString* attributedText = |
156 [[[NSMutableAttributedString alloc] initWithString:strippedText] | 158 [[NSMutableAttributedString alloc] initWithString:strippedText]; |
157 autorelease]; | |
158 | 159 |
159 // Sets the styling to mimic a link. | 160 // Sets the styling to mimic a link. |
160 UIColor* linkColor = UIColorFromRGB(kLinkColorRgb, 1.0); | 161 UIColor* linkColor = UIColorFromRGB(kLinkColorRgb, 1.0); |
161 [attributedText addAttribute:NSForegroundColorAttributeName | 162 [attributedText addAttribute:NSForegroundColorAttributeName |
162 value:linkColor | 163 value:linkColor |
163 range:linkRange]; | 164 range:linkRange]; |
164 [attributedText addAttribute:NSUnderlineStyleAttributeName | 165 [attributedText addAttribute:NSUnderlineStyleAttributeName |
165 value:@(NSUnderlineStyleSingle) | 166 value:@(NSUnderlineStyleSingle) |
166 range:linkRange]; | 167 range:linkRange]; |
167 [attributedText addAttribute:NSUnderlineColorAttributeName | 168 [attributedText addAttribute:NSUnderlineColorAttributeName |
168 value:linkColor | 169 value:linkColor |
169 range:linkRange]; | 170 range:linkRange]; |
170 | 171 |
171 // Sets the line spacing on the attributed string. | 172 // Sets the line spacing on the attributed string. |
172 NSInteger strLength = [strippedText length]; | 173 NSInteger strLength = [strippedText length]; |
173 base::scoped_nsobject<NSMutableParagraphStyle> style( | 174 NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init]; |
174 [[NSMutableParagraphStyle alloc] init]); | |
175 [style setLineSpacing:kLabelLineSpacing]; | 175 [style setLineSpacing:kLabelLineSpacing]; |
176 [attributedText addAttribute:NSParagraphStyleAttributeName | 176 [attributedText addAttribute:NSParagraphStyleAttributeName |
177 value:style | 177 value:style |
178 range:NSMakeRange(0, strLength)]; | 178 range:NSMakeRange(0, strLength)]; |
179 | 179 |
180 [promoLabel setAttributedText:attributedText]; | 180 [promoLabel setAttributedText:attributedText]; |
181 } | 181 } |
182 | 182 |
183 + (UILabel*)promoLabel { | 183 + (UILabel*)promoLabel { |
184 base::scoped_nsobject<UILabel> promoLabel( | 184 UILabel* promoLabel = [[UILabel alloc] initWithFrame:CGRectZero]; |
185 [[UILabel alloc] initWithFrame:CGRectZero]); | |
186 [promoLabel | 185 [promoLabel |
187 setFont:[[MDCTypography fontLoader] regularFontOfSize:kLabelFontSize]]; | 186 setFont:[[MDCTypography fontLoader] regularFontOfSize:kLabelFontSize]]; |
188 [promoLabel setTextColor:UIColorFromRGB(kTextColorRgb, 1.0)]; | 187 [promoLabel setTextColor:UIColorFromRGB(kTextColorRgb, 1.0)]; |
189 [promoLabel setNumberOfLines:0]; | 188 [promoLabel setNumberOfLines:0]; |
190 [promoLabel setTextAlignment:NSTextAlignmentNatural]; | 189 [promoLabel setTextAlignment:NSTextAlignmentNatural]; |
191 [promoLabel setLineBreakMode:NSLineBreakByWordWrapping]; | 190 [promoLabel setLineBreakMode:NSLineBreakByWordWrapping]; |
192 return promoLabel.autorelease(); | 191 return promoLabel; |
193 } | 192 } |
194 | 193 |
195 + (int)heightToFitText:(NSString*)text inWidth:(CGFloat)width { | 194 + (int)heightToFitText:(NSString*)text inWidth:(CGFloat)width { |
196 CGFloat maxWidthForLabel = width - kInfoIconSize - kLabelLeftMargin; | 195 CGFloat maxWidthForLabel = width - kInfoIconSize - kLabelLeftMargin; |
197 base::scoped_nsobject<UILabel> promoLabel([[self promoLabel] retain]); | 196 UILabel* promoLabel = [self promoLabel]; |
198 [[self class] setText:text inPromoLabel:promoLabel.get()]; | 197 [[self class] setText:text inPromoLabel:promoLabel]; |
199 CGFloat promoLabelHeight = | 198 CGFloat promoLabelHeight = |
200 [promoLabel sizeThatFits:CGSizeMake(maxWidthForLabel, CGFLOAT_MAX)] | 199 [promoLabel sizeThatFits:CGSizeMake(maxWidthForLabel, CGFLOAT_MAX)] |
201 .height; | 200 .height; |
202 return promoLabelHeight + kLabelTopMargin + kLabelBottomMargin; | 201 return promoLabelHeight + kLabelTopMargin + kLabelBottomMargin; |
203 } | 202 } |
204 | 203 |
205 @end | 204 @end |
OLD | NEW |