OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/omnibox/truncating_attributed_label.h" | 5 #import "ios/chrome/browser/ui/omnibox/truncating_attributed_label.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/mac/objc_property_releaser.h" | |
10 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
11 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." |
| 13 #endif |
| 14 |
12 @interface OmniboxPopupTruncatingLabel () | 15 @interface OmniboxPopupTruncatingLabel () |
13 - (void)setup; | 16 - (void)setup; |
14 - (UIImage*)getLinearGradient:(CGRect)rect; | 17 - (UIImage*)getLinearGradient:(CGRect)rect; |
15 @end | 18 @end |
16 | 19 |
17 @implementation OmniboxPopupTruncatingLabel { | 20 @implementation OmniboxPopupTruncatingLabel { |
18 // Attributed text. | 21 // Attributed text. |
19 base::scoped_nsobject<CATextLayer> textLayer_; | 22 CATextLayer* textLayer_; |
20 // Gradient used to create fade effect. Changes based on view.frame size. | 23 // Gradient used to create fade effect. Changes based on view.frame size. |
21 base::scoped_nsobject<UIImage> gradient_; | 24 UIImage* gradient_; |
22 | |
23 base::mac::ObjCPropertyReleaser propertyReleaser_OmniboxPopupTruncatingLabel_; | |
24 } | 25 } |
25 | 26 |
26 @synthesize truncateMode = truncateMode_; | 27 @synthesize truncateMode = truncateMode_; |
27 @synthesize attributedText = attributedText_; | 28 @synthesize attributedText = attributedText_; |
28 @synthesize highlighted = highlighted_; | 29 @synthesize highlighted = highlighted_; |
29 @synthesize highlightedText = highlightedText_; | 30 @synthesize highlightedText = highlightedText_; |
30 @synthesize textAlignment = textAlignment_; | 31 @synthesize textAlignment = textAlignment_; |
31 | 32 |
32 - (void)setup { | 33 - (void)setup { |
33 self.backgroundColor = [UIColor clearColor]; | 34 self.backgroundColor = [UIColor clearColor]; |
34 self.contentMode = UIViewContentModeRedraw; | 35 self.contentMode = UIViewContentModeRedraw; |
35 truncateMode_ = OmniboxPopupTruncatingTail; | 36 truncateMode_ = OmniboxPopupTruncatingTail; |
36 | 37 |
37 // Disable animations in CATextLayer. | 38 // Disable animations in CATextLayer. |
38 textLayer_.reset([[CATextLayer layer] retain]); | 39 textLayer_ = [CATextLayer layer]; |
39 base::scoped_nsobject<NSDictionary> actions([[NSDictionary alloc] | 40 NSDictionary* actions = [[NSDictionary alloc] |
40 initWithObjectsAndKeys:[NSNull null], @"contents", nil]); | 41 initWithObjectsAndKeys:[NSNull null], @"contents", nil]; |
41 [textLayer_ setActions:actions]; | 42 [textLayer_ setActions:actions]; |
42 [textLayer_ setFrame:self.bounds]; | 43 [textLayer_ setFrame:self.bounds]; |
43 [textLayer_ setContentsScale:[[UIScreen mainScreen] scale]]; | 44 [textLayer_ setContentsScale:[[UIScreen mainScreen] scale]]; |
44 } | 45 } |
45 | 46 |
46 - (id)initWithFrame:(CGRect)frame { | 47 - (id)initWithFrame:(CGRect)frame { |
47 self = [super initWithFrame:frame]; | 48 self = [super initWithFrame:frame]; |
48 if (self) { | 49 if (self) { |
49 propertyReleaser_OmniboxPopupTruncatingLabel_.Init( | |
50 self, [OmniboxPopupTruncatingLabel class]); | |
51 [self setup]; | 50 [self setup]; |
52 } | 51 } |
53 return self; | 52 return self; |
54 } | 53 } |
55 | 54 |
56 - (void)awakeFromNib { | 55 - (void)awakeFromNib { |
57 [super awakeFromNib]; | 56 [super awakeFromNib]; |
58 [self setup]; | 57 [self setup]; |
59 } | 58 } |
60 | 59 |
61 - (void)setFrame:(CGRect)frame { | 60 - (void)setFrame:(CGRect)frame { |
62 [super setFrame:frame]; | 61 [super setFrame:frame]; |
63 [textLayer_ setFrame:self.bounds]; | 62 [textLayer_ setFrame:self.bounds]; |
64 | 63 |
65 // Cache the fade gradient when the frame changes. | 64 // Cache the fade gradient when the frame changes. |
66 if (!CGRectIsEmpty(frame) && | 65 if (!CGRectIsEmpty(frame) && |
67 (!gradient_.get() || !CGSizeEqualToSize([gradient_ size], frame.size))) { | 66 (!gradient_ || !CGSizeEqualToSize([gradient_ size], frame.size))) { |
68 CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height); | 67 CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height); |
69 gradient_.reset([[self getLinearGradient:rect] retain]); | 68 gradient_ = [self getLinearGradient:rect]; |
70 } | 69 } |
71 } | 70 } |
72 | 71 |
73 - (void)drawRect:(CGRect)rect { | 72 - (void)drawRect:(CGRect)rect { |
74 if ([attributedText_ length] == 0) | 73 if ([attributedText_ length] == 0) |
75 return; | 74 return; |
76 | 75 |
77 CGContextRef context = UIGraphicsGetCurrentContext(); | 76 CGContextRef context = UIGraphicsGetCurrentContext(); |
78 CGContextSaveGState(context); | 77 CGContextSaveGState(context); |
79 [textLayer_ setString:highlighted_ ? highlightedText_ : attributedText_]; | 78 [textLayer_ setString:highlighted_ ? highlightedText_ : attributedText_]; |
(...skipping 10 matching lines...) Expand all Loading... |
90 } else if (textAlignment == NSTextAlignmentRight) { | 89 } else if (textAlignment == NSTextAlignmentRight) { |
91 [textLayer_ setAlignmentMode:kCAAlignmentRight]; | 90 [textLayer_ setAlignmentMode:kCAAlignmentRight]; |
92 self.truncateMode = OmniboxPopupTruncatingHead; | 91 self.truncateMode = OmniboxPopupTruncatingHead; |
93 } else if (textAlignment == NSTextAlignmentNatural) { | 92 } else if (textAlignment == NSTextAlignmentNatural) { |
94 [textLayer_ setAlignmentMode:kCAAlignmentNatural]; | 93 [textLayer_ setAlignmentMode:kCAAlignmentNatural]; |
95 self.truncateMode = OmniboxPopupTruncatingTail; | 94 self.truncateMode = OmniboxPopupTruncatingTail; |
96 } else { | 95 } else { |
97 NOTREACHED(); | 96 NOTREACHED(); |
98 } | 97 } |
99 if (textAlignment != textAlignment_) | 98 if (textAlignment != textAlignment_) |
100 gradient_.reset(); | 99 gradient_ = nil; |
101 textAlignment_ = textAlignment; | 100 textAlignment_ = textAlignment; |
102 } | 101 } |
103 | 102 |
104 // Create gradient opacity mask based on direction. | 103 // Create gradient opacity mask based on direction. |
105 - (UIImage*)getLinearGradient:(CGRect)rect { | 104 - (UIImage*)getLinearGradient:(CGRect)rect { |
106 // Create an opaque context. | 105 // Create an opaque context. |
107 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); | 106 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
108 CGContextRef context = | 107 CGContextRef context = |
109 CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, | 108 CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, |
110 4 * rect.size.width, colorSpace, kCGImageAlphaNone); | 109 4 * rect.size.width, colorSpace, kCGImageAlphaNone); |
(...skipping 30 matching lines...) Expand all Loading... |
141 | 140 |
142 // Clean up, return image. | 141 // Clean up, return image. |
143 CGImageRef ref = CGBitmapContextCreateImage(context); | 142 CGImageRef ref = CGBitmapContextCreateImage(context); |
144 UIImage* image = [UIImage imageWithCGImage:ref]; | 143 UIImage* image = [UIImage imageWithCGImage:ref]; |
145 CGImageRelease(ref); | 144 CGImageRelease(ref); |
146 CGContextRelease(context); | 145 CGContextRelease(context); |
147 return image; | 146 return image; |
148 } | 147 } |
149 | 148 |
150 @end | 149 @end |
OLD | NEW |