| 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 // Gradient used to create fade effect. Changes based on view.frame size. | 21 // Gradient used to create fade effect. Changes based on view.frame size. |
| 19 base::scoped_nsobject<UIImage> gradient_; | 22 UIImage* gradient_; |
| 20 | |
| 21 base::mac::ObjCPropertyReleaser propertyReleaser_OmniboxPopupTruncatingLabel_; | |
| 22 } | 23 } |
| 23 | 24 |
| 24 @synthesize truncateMode = truncateMode_; | 25 @synthesize truncateMode = truncateMode_; |
| 25 | 26 |
| 26 - (void)setup { | 27 - (void)setup { |
| 27 self.backgroundColor = [UIColor clearColor]; | 28 self.backgroundColor = [UIColor clearColor]; |
| 28 truncateMode_ = OmniboxPopupTruncatingTail; | 29 truncateMode_ = OmniboxPopupTruncatingTail; |
| 29 } | 30 } |
| 30 | 31 |
| 31 - (id)initWithFrame:(CGRect)frame { | 32 - (id)initWithFrame:(CGRect)frame { |
| 32 self = [super initWithFrame:frame]; | 33 self = [super initWithFrame:frame]; |
| 33 if (self) { | 34 if (self) { |
| 34 propertyReleaser_OmniboxPopupTruncatingLabel_.Init( | |
| 35 self, [OmniboxPopupTruncatingLabel class]); | |
| 36 self.lineBreakMode = NSLineBreakByClipping; | 35 self.lineBreakMode = NSLineBreakByClipping; |
| 37 [self setup]; | 36 [self setup]; |
| 38 } | 37 } |
| 39 return self; | 38 return self; |
| 40 } | 39 } |
| 41 | 40 |
| 42 - (void)awakeFromNib { | 41 - (void)awakeFromNib { |
| 43 [super awakeFromNib]; | 42 [super awakeFromNib]; |
| 44 [self setup]; | 43 [self setup]; |
| 45 } | 44 } |
| 46 | 45 |
| 47 - (void)setFrame:(CGRect)frame { | 46 - (void)setFrame:(CGRect)frame { |
| 48 [super setFrame:frame]; | 47 [super setFrame:frame]; |
| 49 | 48 |
| 50 // Cache the fade gradient when the frame changes. | 49 // Cache the fade gradient when the frame changes. |
| 51 if (!CGRectIsEmpty(frame) && | 50 if (!CGRectIsEmpty(frame) && |
| 52 (!gradient_.get() || !CGSizeEqualToSize([gradient_ size], frame.size))) { | 51 (!gradient_ || !CGSizeEqualToSize([gradient_ size], frame.size))) { |
| 53 CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height); | 52 CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height); |
| 54 gradient_.reset([[self getLinearGradient:rect] retain]); | 53 gradient_ = [self getLinearGradient:rect]; |
| 55 } | 54 } |
| 56 } | 55 } |
| 57 | 56 |
| 58 // Draw fade gradient mask if attributedText is wider than rect. | 57 // Draw fade gradient mask if attributedText is wider than rect. |
| 59 - (void)drawTextInRect:(CGRect)requestedRect { | 58 - (void)drawTextInRect:(CGRect)requestedRect { |
| 60 CGContextRef context = UIGraphicsGetCurrentContext(); | 59 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 61 CGContextSaveGState(context); | 60 CGContextSaveGState(context); |
| 62 | 61 |
| 63 if ([self.attributedText size].width > requestedRect.size.width) | 62 if ([self.attributedText size].width > requestedRect.size.width) |
| 64 CGContextClipToMask(context, self.bounds, [gradient_ CGImage]); | 63 CGContextClipToMask(context, self.bounds, [gradient_ CGImage]); |
| 65 | 64 |
| 66 // Add the specified line break and alignment attributes to attributedText and | 65 // Add the specified line break and alignment attributes to attributedText and |
| 67 // draw the result. | 66 // draw the result. |
| 68 NSMutableAttributedString* attributedString = | 67 NSMutableAttributedString* attributedString = |
| 69 [[self.attributedText mutableCopy] autorelease]; | 68 [self.attributedText mutableCopy]; |
| 70 NSMutableParagraphStyle* textStyle = | 69 NSMutableParagraphStyle* textStyle = |
| 71 [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease]; | 70 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 72 textStyle.lineBreakMode = self.lineBreakMode; | 71 textStyle.lineBreakMode = self.lineBreakMode; |
| 73 textStyle.alignment = self.textAlignment; | 72 textStyle.alignment = self.textAlignment; |
| 74 [attributedString addAttribute:NSParagraphStyleAttributeName | 73 [attributedString addAttribute:NSParagraphStyleAttributeName |
| 75 value:textStyle | 74 value:textStyle |
| 76 range:NSMakeRange(0, [self.text length])]; | 75 range:NSMakeRange(0, [self.text length])]; |
| 77 [attributedString drawInRect:requestedRect]; | 76 [attributedString drawInRect:requestedRect]; |
| 78 | 77 |
| 79 CGContextRestoreGState(context); | 78 CGContextRestoreGState(context); |
| 80 } | 79 } |
| 81 | 80 |
| 82 - (void)setTextAlignment:(NSTextAlignment)textAlignment { | 81 - (void)setTextAlignment:(NSTextAlignment)textAlignment { |
| 83 if (textAlignment == NSTextAlignmentLeft) { | 82 if (textAlignment == NSTextAlignmentLeft) { |
| 84 self.truncateMode = OmniboxPopupTruncatingTail; | 83 self.truncateMode = OmniboxPopupTruncatingTail; |
| 85 } else if (textAlignment == NSTextAlignmentRight) { | 84 } else if (textAlignment == NSTextAlignmentRight) { |
| 86 self.truncateMode = OmniboxPopupTruncatingHead; | 85 self.truncateMode = OmniboxPopupTruncatingHead; |
| 87 } else if (textAlignment == NSTextAlignmentNatural) { | 86 } else if (textAlignment == NSTextAlignmentNatural) { |
| 88 self.truncateMode = OmniboxPopupTruncatingTail; | 87 self.truncateMode = OmniboxPopupTruncatingTail; |
| 89 } else { | 88 } else { |
| 90 NOTREACHED(); | 89 NOTREACHED(); |
| 91 } | 90 } |
| 92 | 91 |
| 93 if (textAlignment != self.textAlignment) | 92 if (textAlignment != self.textAlignment) |
| 94 gradient_.reset(); | 93 gradient_ = nil; |
| 95 | 94 |
| 96 [super setTextAlignment:textAlignment]; | 95 [super setTextAlignment:textAlignment]; |
| 97 } | 96 } |
| 98 | 97 |
| 99 // Create gradient opacity mask based on direction. | 98 // Create gradient opacity mask based on direction. |
| 100 - (UIImage*)getLinearGradient:(CGRect)rect { | 99 - (UIImage*)getLinearGradient:(CGRect)rect { |
| 101 // Create an opaque context. | 100 // Create an opaque context. |
| 102 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); | 101 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
| 103 CGContextRef context = | 102 CGContextRef context = |
| 104 CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, | 103 CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 135 |
| 137 // Clean up, return image. | 136 // Clean up, return image. |
| 138 CGImageRef ref = CGBitmapContextCreateImage(context); | 137 CGImageRef ref = CGBitmapContextCreateImage(context); |
| 139 UIImage* image = [UIImage imageWithCGImage:ref]; | 138 UIImage* image = [UIImage imageWithCGImage:ref]; |
| 140 CGImageRelease(ref); | 139 CGImageRelease(ref); |
| 141 CGContextRelease(context); | 140 CGContextRelease(context); |
| 142 return image; | 141 return image; |
| 143 } | 142 } |
| 144 | 143 |
| 145 @end | 144 @end |
| OLD | NEW |