Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: ios/chrome/browser/ui/omnibox/truncating_attributed_label.mm

Issue 2695413003: Change OmniboxPopupTruncatingLabel to be based on UILabel. (Closed)
Patch Set: Cache gradient, restore handling of text alignment. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/chrome/browser/ui/omnibox/truncating_attributed_label.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 9 #include "base/mac/objc_property_releaser.h"
10 #include "base/mac/scoped_cftyperef.h" 10 #include "base/mac/scoped_cftyperef.h"
11 11
12 @interface OmniboxPopupTruncatingLabel () 12 @interface OmniboxPopupTruncatingLabel ()
13 - (void)setup; 13 - (void)setup;
14 - (UIImage*)getLinearGradient:(CGRect)rect; 14 - (UIImage*)getLinearGradient:(CGRect)rect;
15 @end 15 @end
16 16
17 @implementation OmniboxPopupTruncatingLabel { 17 @implementation OmniboxPopupTruncatingLabel {
18 // Attributed text.
19 base::scoped_nsobject<CATextLayer> textLayer_;
20 // Gradient used to create fade effect. Changes based on view.frame size. 18 // Gradient used to create fade effect. Changes based on view.frame size.
21 base::scoped_nsobject<UIImage> gradient_; 19 base::scoped_nsobject<UIImage> gradient_;
22 20
23 base::mac::ObjCPropertyReleaser propertyReleaser_OmniboxPopupTruncatingLabel_; 21 base::mac::ObjCPropertyReleaser propertyReleaser_OmniboxPopupTruncatingLabel_;
24 } 22 }
25 23
26 @synthesize truncateMode = truncateMode_; 24 @synthesize truncateMode = truncateMode_;
27 @synthesize attributedText = attributedText_;
28 @synthesize highlighted = highlighted_;
29 @synthesize highlightedText = highlightedText_;
30 @synthesize textAlignment = textAlignment_;
31 25
32 - (void)setup { 26 - (void)setup {
33 self.backgroundColor = [UIColor clearColor]; 27 self.backgroundColor = [UIColor clearColor];
34 self.contentMode = UIViewContentModeRedraw;
35 truncateMode_ = OmniboxPopupTruncatingTail; 28 truncateMode_ = OmniboxPopupTruncatingTail;
36
37 // Disable animations in CATextLayer.
38 textLayer_.reset([[CATextLayer layer] retain]);
39 base::scoped_nsobject<NSDictionary> actions([[NSDictionary alloc]
40 initWithObjectsAndKeys:[NSNull null], @"contents", nil]);
41 [textLayer_ setActions:actions];
42 [textLayer_ setFrame:self.bounds];
43 [textLayer_ setContentsScale:[[UIScreen mainScreen] scale]];
44 } 29 }
45 30
46 - (id)initWithFrame:(CGRect)frame { 31 - (id)initWithFrame:(CGRect)frame {
47 self = [super initWithFrame:frame]; 32 self = [super initWithFrame:frame];
48 if (self) { 33 if (self) {
49 propertyReleaser_OmniboxPopupTruncatingLabel_.Init( 34 propertyReleaser_OmniboxPopupTruncatingLabel_.Init(
50 self, [OmniboxPopupTruncatingLabel class]); 35 self, [OmniboxPopupTruncatingLabel class]);
36 self.lineBreakMode = NSLineBreakByClipping;
51 [self setup]; 37 [self setup];
52 } 38 }
53 return self; 39 return self;
54 } 40 }
55 41
56 - (void)awakeFromNib { 42 - (void)awakeFromNib {
57 [super awakeFromNib]; 43 [super awakeFromNib];
58 [self setup]; 44 [self setup];
59 } 45 }
60 46
61 - (void)setFrame:(CGRect)frame { 47 - (void)setFrame:(CGRect)frame {
62 [super setFrame:frame]; 48 [super setFrame:frame];
63 [textLayer_ setFrame:self.bounds];
64 49
65 // Cache the fade gradient when the frame changes. 50 // Cache the fade gradient when the frame changes.
66 if (!CGRectIsEmpty(frame) && 51 if (!CGRectIsEmpty(frame) &&
67 (!gradient_.get() || !CGSizeEqualToSize([gradient_ size], frame.size))) { 52 (!gradient_.get() || !CGSizeEqualToSize([gradient_ size], frame.size))) {
68 CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height); 53 CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
69 gradient_.reset([[self getLinearGradient:rect] retain]); 54 gradient_.reset([[self getLinearGradient:rect] retain]);
70 } 55 }
71 } 56 }
72 57
73 - (void)drawRect:(CGRect)rect { 58 // Draw fade gradient mask if attributedText is wider than rect.
74 if ([attributedText_ length] == 0) 59 - (void)drawTextInRect:(CGRect)requestedRect {
75 return;
76
77 CGContextRef context = UIGraphicsGetCurrentContext(); 60 CGContextRef context = UIGraphicsGetCurrentContext();
78 CGContextSaveGState(context); 61 CGContextSaveGState(context);
79 [textLayer_ setString:highlighted_ ? highlightedText_ : attributedText_]; 62
80 CGContextClipToMask(context, self.bounds, [gradient_ CGImage]); 63 if ([self.attributedText size].width > requestedRect.size.width)
81 [textLayer_ renderInContext:context]; 64 CGContextClipToMask(context, self.bounds, [gradient_ CGImage]);
65
66 // Add the specified line break and alignment attributes to attributedText and
67 // draw the result.
68 NSMutableAttributedString* attributedString =
69 [[self.attributedText mutableCopy] autorelease];
70 NSMutableParagraphStyle* textStyle =
71 [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
72 textStyle.lineBreakMode = self.lineBreakMode;
73 textStyle.alignment = self.textAlignment;
74 [attributedString addAttribute:NSParagraphStyleAttributeName
75 value:textStyle
76 range:NSMakeRange(0, [self.text length])];
77 [attributedString drawInRect:requestedRect];
82 78
83 CGContextRestoreGState(context); 79 CGContextRestoreGState(context);
84 } 80 }
85 81
86 - (void)setTextAlignment:(NSTextAlignment)textAlignment { 82 - (void)setTextAlignment:(NSTextAlignment)textAlignment {
87 if (textAlignment == NSTextAlignmentLeft) { 83 if (textAlignment == NSTextAlignmentLeft) {
88 [textLayer_ setAlignmentMode:kCAAlignmentLeft];
89 self.truncateMode = OmniboxPopupTruncatingTail; 84 self.truncateMode = OmniboxPopupTruncatingTail;
90 } else if (textAlignment == NSTextAlignmentRight) { 85 } else if (textAlignment == NSTextAlignmentRight) {
91 [textLayer_ setAlignmentMode:kCAAlignmentRight];
92 self.truncateMode = OmniboxPopupTruncatingHead; 86 self.truncateMode = OmniboxPopupTruncatingHead;
93 } else if (textAlignment == NSTextAlignmentNatural) { 87 } else if (textAlignment == NSTextAlignmentNatural) {
94 [textLayer_ setAlignmentMode:kCAAlignmentNatural];
95 self.truncateMode = OmniboxPopupTruncatingTail; 88 self.truncateMode = OmniboxPopupTruncatingTail;
96 } else { 89 } else {
97 NOTREACHED(); 90 NOTREACHED();
98 } 91 }
99 if (textAlignment != textAlignment_) 92
93 if (textAlignment != self.textAlignment)
100 gradient_.reset(); 94 gradient_.reset();
101 textAlignment_ = textAlignment; 95
96 [super setTextAlignment:textAlignment];
102 } 97 }
103 98
104 // Create gradient opacity mask based on direction. 99 // Create gradient opacity mask based on direction.
105 - (UIImage*)getLinearGradient:(CGRect)rect { 100 - (UIImage*)getLinearGradient:(CGRect)rect {
106 // Create an opaque context. 101 // Create an opaque context.
107 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 102 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
108 CGContextRef context = 103 CGContextRef context =
109 CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 104 CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8,
110 4 * rect.size.width, colorSpace, kCGImageAlphaNone); 105 4 * rect.size.width, colorSpace, kCGImageAlphaNone);
111 106
(...skipping 29 matching lines...) Expand all
141 136
142 // Clean up, return image. 137 // Clean up, return image.
143 CGImageRef ref = CGBitmapContextCreateImage(context); 138 CGImageRef ref = CGBitmapContextCreateImage(context);
144 UIImage* image = [UIImage imageWithCGImage:ref]; 139 UIImage* image = [UIImage imageWithCGImage:ref];
145 CGImageRelease(ref); 140 CGImageRelease(ref);
146 CGContextRelease(context); 141 CGContextRelease(context);
147 return image; 142 return image;
148 } 143 }
149 144
150 @end 145 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/omnibox/truncating_attributed_label.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698