| 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/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
| 10 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." | 12 #error "This file requires ARC support." |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 @interface OmniboxPopupTruncatingLabel () | 15 @interface OmniboxPopupTruncatingLabel () |
| 16 - (void)setup; | 16 - (void)setup; |
| 17 - (UIImage*)getLinearGradient:(CGRect)rect; | 17 - (UIImage*)getLinearGradient:(CGRect)rect; |
| 18 @end | 18 @end |
| 19 | 19 |
| 20 @implementation OmniboxPopupTruncatingLabel { | 20 @implementation OmniboxPopupTruncatingLabel { |
| 21 // 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. |
| 22 UIImage* gradient_; | 22 UIImage* gradient_; |
| 23 } | 23 } |
| 24 | 24 |
| 25 @synthesize truncateMode = truncateMode_; | 25 @synthesize truncateMode = truncateMode_; |
| 26 @synthesize displayAsURL = displayAsURL_; |
| 26 | 27 |
| 27 - (void)setup { | 28 - (void)setup { |
| 28 self.backgroundColor = [UIColor clearColor]; | 29 self.backgroundColor = [UIColor clearColor]; |
| 29 truncateMode_ = OmniboxPopupTruncatingTail; | 30 truncateMode_ = OmniboxPopupTruncatingTail; |
| 30 } | 31 } |
| 31 | 32 |
| 32 - (id)initWithFrame:(CGRect)frame { | 33 - (id)initWithFrame:(CGRect)frame { |
| 33 self = [super initWithFrame:frame]; | 34 self = [super initWithFrame:frame]; |
| 34 if (self) { | 35 if (self) { |
| 35 self.lineBreakMode = NSLineBreakByClipping; | 36 self.lineBreakMode = NSLineBreakByClipping; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 63 CGContextClipToMask(context, self.bounds, [gradient_ CGImage]); | 64 CGContextClipToMask(context, self.bounds, [gradient_ CGImage]); |
| 64 | 65 |
| 65 // Add the specified line break and alignment attributes to attributedText and | 66 // Add the specified line break and alignment attributes to attributedText and |
| 66 // draw the result. | 67 // draw the result. |
| 67 NSMutableAttributedString* attributedString = | 68 NSMutableAttributedString* attributedString = |
| 68 [self.attributedText mutableCopy]; | 69 [self.attributedText mutableCopy]; |
| 69 NSMutableParagraphStyle* textStyle = | 70 NSMutableParagraphStyle* textStyle = |
| 70 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; | 71 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 71 textStyle.lineBreakMode = self.lineBreakMode; | 72 textStyle.lineBreakMode = self.lineBreakMode; |
| 72 textStyle.alignment = self.textAlignment; | 73 textStyle.alignment = self.textAlignment; |
| 74 // URLs have their text direction set to to LTR (avoids RTL characters |
| 75 // making the URL render from right to left, as per RFC 3987 Section 4.1). |
| 76 if (self.displayAsURL) |
| 77 textStyle.baseWritingDirection = NSWritingDirectionLeftToRight; |
| 73 [attributedString addAttribute:NSParagraphStyleAttributeName | 78 [attributedString addAttribute:NSParagraphStyleAttributeName |
| 74 value:textStyle | 79 value:textStyle |
| 75 range:NSMakeRange(0, [self.text length])]; | 80 range:NSMakeRange(0, [self.text length])]; |
| 76 [attributedString drawInRect:requestedRect]; | 81 [attributedString drawInRect:requestedRect]; |
| 77 | 82 |
| 78 CGContextRestoreGState(context); | 83 CGContextRestoreGState(context); |
| 79 } | 84 } |
| 80 | 85 |
| 81 - (void)setTextAlignment:(NSTextAlignment)textAlignment { | 86 - (void)setTextAlignment:(NSTextAlignment)textAlignment { |
| 82 if (textAlignment == NSTextAlignmentLeft) { | 87 if (textAlignment == NSTextAlignmentLeft) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 140 |
| 136 // Clean up, return image. | 141 // Clean up, return image. |
| 137 CGImageRef ref = CGBitmapContextCreateImage(context); | 142 CGImageRef ref = CGBitmapContextCreateImage(context); |
| 138 UIImage* image = [UIImage imageWithCGImage:ref]; | 143 UIImage* image = [UIImage imageWithCGImage:ref]; |
| 139 CGImageRelease(ref); | 144 CGImageRelease(ref); |
| 140 CGContextRelease(context); | 145 CGContextRelease(context); |
| 141 return image; | 146 return image; |
| 142 } | 147 } |
| 143 | 148 |
| 144 @end | 149 @end |
| OLD | NEW |