| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/fade_truncated_label.h" | 5 #import "ios/chrome/browser/ui/fade_truncated_label.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include "base/mac/objc_property_releaser.h" | 8 |
| 9 #import "ios/chrome/browser/ui/animation_util.h" | 9 #import "ios/chrome/browser/ui/animation_util.h" |
| 10 #import "ios/chrome/browser/ui/reversed_animation.h" | 10 #import "ios/chrome/browser/ui/reversed_animation.h" |
| 11 #import "ui/gfx/ios/uikit_util.h" | 11 #import "ui/gfx/ios/uikit_util.h" |
| 12 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 13 namespace { | 17 namespace { |
| 14 // Animation key used for frame animations. | 18 // Animation key used for frame animations. |
| 15 NSString* const kFadeTruncatedLabelAnimationKey = | 19 NSString* const kFadeTruncatedLabelAnimationKey = |
| 16 @"FadeTruncatedLabelAnimationKey"; | 20 @"FadeTruncatedLabelAnimationKey"; |
| 17 } | 21 } |
| 18 | 22 |
| 19 @interface FadeTruncatedLabel () { | 23 @interface FadeTruncatedLabel () { |
| 20 base::mac::ObjCPropertyReleaser _propertyReleaser_FadeTruncatedLabel; | |
| 21 } | 24 } |
| 22 | 25 |
| 23 // Layer used to apply fade truncation to label. | 26 // Layer used to apply fade truncation to label. |
| 24 @property(nonatomic, retain) CAGradientLayer* maskLayer; | 27 @property(nonatomic, strong) CAGradientLayer* maskLayer; |
| 25 | 28 |
| 26 // Temporary label used during animations. | 29 // Temporary label used during animations. |
| 27 @property(nonatomic, retain) UILabel* animationLabel; | 30 @property(nonatomic, strong) UILabel* animationLabel; |
| 28 | 31 |
| 29 // Returns the percentage of the label's width at which to begin the fade | 32 // Returns the percentage of the label's width at which to begin the fade |
| 30 // gradient. | 33 // gradient. |
| 31 + (CGFloat)gradientPercentageForText:(NSString*)text | 34 + (CGFloat)gradientPercentageForText:(NSString*)text |
| 32 withAttributes:(NSDictionary*)attributes | 35 withAttributes:(NSDictionary*)attributes |
| 33 inBounds:(CGRect)bounds; | 36 inBounds:(CGRect)bounds; |
| 34 | 37 |
| 35 @end | 38 @end |
| 36 | 39 |
| 37 @implementation FadeTruncatedLabel | 40 @implementation FadeTruncatedLabel |
| 38 | 41 |
| 39 @synthesize maskLayer = _maskLayer; | 42 @synthesize maskLayer = _maskLayer; |
| 40 @synthesize animationLabel = _animationLabel; | 43 @synthesize animationLabel = _animationLabel; |
| 41 | 44 |
| 42 - (instancetype)initWithFrame:(CGRect)frame { | 45 - (instancetype)initWithFrame:(CGRect)frame { |
| 43 self = [super initWithFrame:frame]; | 46 self = [super initWithFrame:frame]; |
| 44 if (self) { | 47 if (self) { |
| 45 // Initialize property releaser. | |
| 46 _propertyReleaser_FadeTruncatedLabel.Init(self, [FadeTruncatedLabel class]); | |
| 47 | |
| 48 // Set background color and line break mode. | 48 // Set background color and line break mode. |
| 49 self.backgroundColor = [UIColor clearColor]; | 49 self.backgroundColor = [UIColor clearColor]; |
| 50 self.lineBreakMode = NSLineBreakByClipping; | 50 self.lineBreakMode = NSLineBreakByClipping; |
| 51 | 51 |
| 52 // Instantiate |maskLayer| and add as mask. | 52 // Instantiate |maskLayer| and add as mask. |
| 53 self.maskLayer = [[self class] maskLayerForText:self.text | 53 self.maskLayer = [[self class] maskLayerForText:self.text |
| 54 withAttributes:@{ | 54 withAttributes:@{ |
| 55 NSFontAttributeName : self.font | 55 NSFontAttributeName : self.font |
| 56 } | 56 } |
| 57 inBounds:{CGPointZero, frame.size}]; | 57 inBounds:{CGPointZero, frame.size}]; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 frameAnimation.timingFunction = timingFunction; | 94 frameAnimation.timingFunction = timingFunction; |
| 95 [self.layer addAnimation:frameAnimation | 95 [self.layer addAnimation:frameAnimation |
| 96 forKey:kFadeTruncatedLabelAnimationKey]; | 96 forKey:kFadeTruncatedLabelAnimationKey]; |
| 97 | 97 |
| 98 // When animating, add a temporary label using the larger frame size so that | 98 // When animating, add a temporary label using the larger frame size so that |
| 99 // truncation can occur via the gradient rather than by the edge of the text | 99 // truncation can occur via the gradient rather than by the edge of the text |
| 100 // layer's backing store. | 100 // layer's backing store. |
| 101 CGSize animationTextSize = | 101 CGSize animationTextSize = |
| 102 CGSizeMake(std::max(beginFrame.size.width, endFrame.size.width), | 102 CGSizeMake(std::max(beginFrame.size.width, endFrame.size.width), |
| 103 std::max(beginFrame.size.height, endFrame.size.height)); | 103 std::max(beginFrame.size.height, endFrame.size.height)); |
| 104 self.animationLabel = [[[UILabel alloc] | 104 self.animationLabel = |
| 105 initWithFrame:{CGPointZero, animationTextSize}] autorelease]; | 105 [[UILabel alloc] initWithFrame:{CGPointZero, animationTextSize}]; |
| 106 self.animationLabel.text = self.text; | 106 self.animationLabel.text = self.text; |
| 107 self.animationLabel.textColor = self.textColor; | 107 self.animationLabel.textColor = self.textColor; |
| 108 self.animationLabel.font = self.font; | 108 self.animationLabel.font = self.font; |
| 109 [self addSubview:self.animationLabel]; | 109 [self addSubview:self.animationLabel]; |
| 110 | 110 |
| 111 // Animate the mask layer. | 111 // Animate the mask layer. |
| 112 CGRect beginBounds = {CGPointZero, beginFrame.size}; | 112 CGRect beginBounds = {CGPointZero, beginFrame.size}; |
| 113 CGRect endBounds = {CGPointZero, endFrame.size}; | 113 CGRect endBounds = {CGPointZero, endFrame.size}; |
| 114 frameAnimation = FrameAnimationMake(self.maskLayer, beginBounds, endBounds); | 114 frameAnimation = FrameAnimationMake(self.maskLayer, beginBounds, endBounds); |
| 115 frameAnimation.duration = duration; | 115 frameAnimation.duration = duration; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 maskLayer.startPoint = CGPointMake(0.0, 0.5); | 181 maskLayer.startPoint = CGPointMake(0.0, 0.5); |
| 182 maskLayer.endPoint = CGPointMake(1.0, 0.5); | 182 maskLayer.endPoint = CGPointMake(1.0, 0.5); |
| 183 CGFloat gradientBeginPercentage = [self gradientPercentageForText:text | 183 CGFloat gradientBeginPercentage = [self gradientPercentageForText:text |
| 184 withAttributes:attributes | 184 withAttributes:attributes |
| 185 inBounds:bounds]; | 185 inBounds:bounds]; |
| 186 maskLayer.locations = @[ @(gradientBeginPercentage), @(1.0) ]; | 186 maskLayer.locations = @[ @(gradientBeginPercentage), @(1.0) ]; |
| 187 return maskLayer; | 187 return maskLayer; |
| 188 } | 188 } |
| 189 | 189 |
| 190 @end | 190 @end |
| OLD | NEW |