| 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 #include "base/mac/objc_release_properties.h" |
| 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 namespace { | 13 namespace { |
| 14 // Animation key used for frame animations. | 14 // Animation key used for frame animations. |
| 15 NSString* const kFadeTruncatedLabelAnimationKey = | 15 NSString* const kFadeTruncatedLabelAnimationKey = |
| 16 @"FadeTruncatedLabelAnimationKey"; | 16 @"FadeTruncatedLabelAnimationKey"; |
| 17 } | 17 } |
| 18 | 18 |
| 19 @interface FadeTruncatedLabel () { | 19 @interface FadeTruncatedLabel () |
| 20 base::mac::ObjCPropertyReleaser _propertyReleaser_FadeTruncatedLabel; | |
| 21 } | |
| 22 | 20 |
| 23 // Layer used to apply fade truncation to label. | 21 // Layer used to apply fade truncation to label. |
| 24 @property(nonatomic, retain) CAGradientLayer* maskLayer; | 22 @property(nonatomic, retain) CAGradientLayer* maskLayer; |
| 25 | 23 |
| 26 // Temporary label used during animations. | 24 // Temporary label used during animations. |
| 27 @property(nonatomic, retain) UILabel* animationLabel; | 25 @property(nonatomic, retain) UILabel* animationLabel; |
| 28 | 26 |
| 29 // Returns the percentage of the label's width at which to begin the fade | 27 // Returns the percentage of the label's width at which to begin the fade |
| 30 // gradient. | 28 // gradient. |
| 31 + (CGFloat)gradientPercentageForText:(NSString*)text | 29 + (CGFloat)gradientPercentageForText:(NSString*)text |
| 32 withAttributes:(NSDictionary*)attributes | 30 withAttributes:(NSDictionary*)attributes |
| 33 inBounds:(CGRect)bounds; | 31 inBounds:(CGRect)bounds; |
| 34 | 32 |
| 35 @end | 33 @end |
| 36 | 34 |
| 37 @implementation FadeTruncatedLabel | 35 @implementation FadeTruncatedLabel |
| 38 | 36 |
| 39 @synthesize maskLayer = _maskLayer; | 37 @synthesize maskLayer = _maskLayer; |
| 40 @synthesize animationLabel = _animationLabel; | 38 @synthesize animationLabel = _animationLabel; |
| 41 | 39 |
| 42 - (instancetype)initWithFrame:(CGRect)frame { | 40 - (instancetype)initWithFrame:(CGRect)frame { |
| 43 self = [super initWithFrame:frame]; | 41 self = [super initWithFrame:frame]; |
| 44 if (self) { | 42 if (self) { |
| 45 // Initialize property releaser. | |
| 46 _propertyReleaser_FadeTruncatedLabel.Init(self, [FadeTruncatedLabel class]); | |
| 47 | |
| 48 // Set background color and line break mode. | 43 // Set background color and line break mode. |
| 49 self.backgroundColor = [UIColor clearColor]; | 44 self.backgroundColor = [UIColor clearColor]; |
| 50 self.lineBreakMode = NSLineBreakByClipping; | 45 self.lineBreakMode = NSLineBreakByClipping; |
| 51 | 46 |
| 52 // Instantiate |maskLayer| and add as mask. | 47 // Instantiate |maskLayer| and add as mask. |
| 53 self.maskLayer = [[self class] maskLayerForText:self.text | 48 self.maskLayer = [[self class] maskLayerForText:self.text |
| 54 withAttributes:@{ | 49 withAttributes:@{ |
| 55 NSFontAttributeName : self.font | 50 NSFontAttributeName : self.font |
| 56 } | 51 } |
| 57 inBounds:{CGPointZero, frame.size}]; | 52 inBounds:{CGPointZero, frame.size}]; |
| 58 self.layer.mask = self.maskLayer; | 53 self.layer.mask = self.maskLayer; |
| 59 } | 54 } |
| 60 return self; | 55 return self; |
| 61 } | 56 } |
| 62 | 57 |
| 58 - (void)dealloc { |
| 59 base::mac::ReleaseProperties(self); |
| 60 [super dealloc]; |
| 61 } |
| 62 |
| 63 #pragma mark - UILabel overrides | 63 #pragma mark - UILabel overrides |
| 64 | 64 |
| 65 - (void)drawTextInRect:(CGRect)rect { | 65 - (void)drawTextInRect:(CGRect)rect { |
| 66 // Draw if not animating. | 66 // Draw if not animating. |
| 67 if (!self.animationLabel) | 67 if (!self.animationLabel) |
| 68 [super drawTextInRect:rect]; | 68 [super drawTextInRect:rect]; |
| 69 | 69 |
| 70 // Update the mask gradient. | 70 // Update the mask gradient. |
| 71 [CATransaction begin]; | 71 [CATransaction begin]; |
| 72 [CATransaction setDisableActions:YES]; | 72 [CATransaction setDisableActions:YES]; |
| (...skipping 108 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 |