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 "ui/gfx/ios/NSString+CrStringDrawing.h" | 5 #import "ui/gfx/ios/NSString+CrStringDrawing.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/gfx/ios/uikit_util.h" | 8 #include "ui/gfx/ios/uikit_util.h" |
9 | 9 |
10 @implementation NSString (CrStringDrawing) | 10 @implementation NSString (CrStringDrawing) |
(...skipping 24 matching lines...) Expand all Loading... |
35 NSDictionary* attributes = @{ NSFontAttributeName : font }; | 35 NSDictionary* attributes = @{ NSFontAttributeName : font }; |
36 CGSize size = [self sizeWithAttributes:attributes]; | 36 CGSize size = [self sizeWithAttributes:attributes]; |
37 return CGSizeMake(ceil(size.width), ceil(size.height)); | 37 return CGSizeMake(ceil(size.width), ceil(size.height)); |
38 } | 38 } |
39 | 39 |
40 - (NSString*)cr_stringByCuttingToIndex:(NSUInteger)index { | 40 - (NSString*)cr_stringByCuttingToIndex:(NSUInteger)index { |
41 if (index == 0) | 41 if (index == 0) |
42 return @""; | 42 return @""; |
43 if (index >= [self length]) | 43 if (index >= [self length]) |
44 return [[self retain] autorelease]; | 44 return [[self retain] autorelease]; |
45 return [[self substringToIndex:index - 1] stringByAppendingString:@"…"]; | 45 return [[self substringToIndex:(index - 1)] stringByAppendingString:@"…"]; |
46 } | 46 } |
47 | 47 |
48 - (NSString*)cr_stringByElidingToFitSize:(CGSize)bounds { | 48 - (NSString*)cr_stringByElidingToFitSize:(CGSize)bounds { |
49 CGSize sizeForGuess = CGSizeMake(bounds.width, CGFLOAT_MAX); | 49 CGSize sizeForGuess = CGSizeMake(bounds.width, CGFLOAT_MAX); |
50 // Use binary search on the string's length. | 50 // Use binary search on the string's length. |
51 size_t lo = 0; | 51 size_t lo = 0; |
52 size_t hi = [self length]; | 52 size_t hi = [self length]; |
53 size_t guess = 0; | 53 size_t guess = 0; |
54 for (guess = (lo + hi) / 2; lo < hi; guess = (lo + hi) / 2) { | 54 for (guess = (lo + hi) / 2; lo < hi; guess = (lo + hi) / 2) { |
55 NSString* tempString = [self cr_stringByCuttingToIndex:guess]; | 55 NSString* tempString = [self cr_stringByCuttingToIndex:guess]; |
56 UIFont* font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; | 56 UIFont* font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
57 CGSize sizeGuess = | 57 CGSize sizeGuess = |
58 [tempString cr_boundingSizeWithSize:sizeForGuess font:font]; | 58 [tempString cr_boundingSizeWithSize:sizeForGuess font:font]; |
59 if (sizeGuess.height > bounds.height) { | 59 if (sizeGuess.height > bounds.height) { |
60 hi = guess - 1; | 60 hi = guess - 1; |
61 if (hi < lo) | 61 if (hi < lo) |
62 hi = lo; | 62 hi = lo; |
63 } else { | 63 } else { |
64 lo = guess + 1; | 64 lo = guess + 1; |
65 } | 65 } |
66 } | 66 } |
67 return [self cr_stringByCuttingToIndex:lo]; | 67 return [self cr_stringByCuttingToIndex:lo]; |
68 } | 68 } |
69 | 69 |
70 @end | 70 @end |
OLD | NEW |