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 } | |
22 | 24 |
23 // Layer used to apply fade truncation to label. | 25 // Layer used to apply fade truncation to label. |
24 @property(nonatomic, retain) CAGradientLayer* maskLayer; | 26 @property(nonatomic, strong) CAGradientLayer* maskLayer; |
25 | 27 |
26 // Temporary label used during animations. | 28 // Temporary label used during animations. |
27 @property(nonatomic, retain) UILabel* animationLabel; | 29 @property(nonatomic, strong) UILabel* animationLabel; |
28 | 30 |
29 // Returns the percentage of the label's width at which to begin the fade | 31 // Returns the percentage of the label's width at which to begin the fade |
30 // gradient. | 32 // gradient. |
31 + (CGFloat)gradientPercentageForText:(NSString*)text | 33 + (CGFloat)gradientPercentageForText:(NSString*)text |
32 withAttributes:(NSDictionary*)attributes | 34 withAttributes:(NSDictionary*)attributes |
33 inBounds:(CGRect)bounds; | 35 inBounds:(CGRect)bounds; |
34 | 36 |
35 @end | 37 @end |
36 | 38 |
37 @implementation FadeTruncatedLabel | 39 @implementation FadeTruncatedLabel |
38 | 40 |
39 @synthesize maskLayer = _maskLayer; | 41 @synthesize maskLayer = _maskLayer; |
40 @synthesize animationLabel = _animationLabel; | 42 @synthesize animationLabel = _animationLabel; |
41 | 43 |
42 - (instancetype)initWithFrame:(CGRect)frame { | 44 - (instancetype)initWithFrame:(CGRect)frame { |
43 self = [super initWithFrame:frame]; | 45 self = [super initWithFrame:frame]; |
44 if (self) { | 46 if (self) { |
45 // Initialize property releaser. | |
46 _propertyReleaser_FadeTruncatedLabel.Init(self, [FadeTruncatedLabel class]); | |
47 | |
48 // Set background color and line break mode. | 47 // Set background color and line break mode. |
49 self.backgroundColor = [UIColor clearColor]; | 48 self.backgroundColor = [UIColor clearColor]; |
50 self.lineBreakMode = NSLineBreakByClipping; | 49 self.lineBreakMode = NSLineBreakByClipping; |
51 | 50 |
52 // Instantiate |maskLayer| and add as mask. | 51 // Instantiate |maskLayer| and add as mask. |
53 self.maskLayer = [[self class] maskLayerForText:self.text | 52 self.maskLayer = [[self class] maskLayerForText:self.text |
54 withAttributes:@{ | 53 withAttributes:@{ |
55 NSFontAttributeName : self.font | 54 NSFontAttributeName : self.font |
56 } | 55 } |
57 inBounds:{CGPointZero, frame.size}]; | 56 inBounds:{CGPointZero, frame.size}]; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 frameAnimation.timingFunction = timingFunction; | 93 frameAnimation.timingFunction = timingFunction; |
95 [self.layer addAnimation:frameAnimation | 94 [self.layer addAnimation:frameAnimation |
96 forKey:kFadeTruncatedLabelAnimationKey]; | 95 forKey:kFadeTruncatedLabelAnimationKey]; |
97 | 96 |
98 // When animating, add a temporary label using the larger frame size so that | 97 // 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 | 98 // truncation can occur via the gradient rather than by the edge of the text |
100 // layer's backing store. | 99 // layer's backing store. |
101 CGSize animationTextSize = | 100 CGSize animationTextSize = |
102 CGSizeMake(std::max(beginFrame.size.width, endFrame.size.width), | 101 CGSizeMake(std::max(beginFrame.size.width, endFrame.size.width), |
103 std::max(beginFrame.size.height, endFrame.size.height)); | 102 std::max(beginFrame.size.height, endFrame.size.height)); |
104 self.animationLabel = [[[UILabel alloc] | 103 self.animationLabel = |
105 initWithFrame:{CGPointZero, animationTextSize}] autorelease]; | 104 [[UILabel alloc] initWithFrame:{CGPointZero, animationTextSize}]; |
106 self.animationLabel.text = self.text; | 105 self.animationLabel.text = self.text; |
107 self.animationLabel.textColor = self.textColor; | 106 self.animationLabel.textColor = self.textColor; |
108 self.animationLabel.font = self.font; | 107 self.animationLabel.font = self.font; |
109 [self addSubview:self.animationLabel]; | 108 [self addSubview:self.animationLabel]; |
110 | 109 |
111 // Animate the mask layer. | 110 // Animate the mask layer. |
112 CGRect beginBounds = {CGPointZero, beginFrame.size}; | 111 CGRect beginBounds = {CGPointZero, beginFrame.size}; |
113 CGRect endBounds = {CGPointZero, endFrame.size}; | 112 CGRect endBounds = {CGPointZero, endFrame.size}; |
114 frameAnimation = FrameAnimationMake(self.maskLayer, beginBounds, endBounds); | 113 frameAnimation = FrameAnimationMake(self.maskLayer, beginBounds, endBounds); |
115 frameAnimation.duration = duration; | 114 frameAnimation.duration = duration; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 maskLayer.startPoint = CGPointMake(0.0, 0.5); | 180 maskLayer.startPoint = CGPointMake(0.0, 0.5); |
182 maskLayer.endPoint = CGPointMake(1.0, 0.5); | 181 maskLayer.endPoint = CGPointMake(1.0, 0.5); |
183 CGFloat gradientBeginPercentage = [self gradientPercentageForText:text | 182 CGFloat gradientBeginPercentage = [self gradientPercentageForText:text |
184 withAttributes:attributes | 183 withAttributes:attributes |
185 inBounds:bounds]; | 184 inBounds:bounds]; |
186 maskLayer.locations = @[ @(gradientBeginPercentage), @(1.0) ]; | 185 maskLayer.locations = @[ @(gradientBeginPercentage), @(1.0) ]; |
187 return maskLayer; | 186 return maskLayer; |
188 } | 187 } |
189 | 188 |
190 @end | 189 @end |
OLD | NEW |