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