Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 30 } | 30 } |
| 31 | 31 |
| 32 - (CGSize)cr_sizeWithFont:(UIFont*)font { | 32 - (CGSize)cr_sizeWithFont:(UIFont*)font { |
| 33 if (!font) | 33 if (!font) |
| 34 return CGSizeZero; | 34 return CGSizeZero; |
| 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 { | |
| 41 if (index == 0) | |
| 42 return @""; | |
| 43 if (index >= [self length]) | |
| 44 return [[self retain] autorelease]; | |
| 45 return [[self substringToIndex:index - 1] stringByAppendingString:@"…"]; | |
|
rohitrao (ping after 24h)
2015/03/10 18:35:53
Parens around (index - 1), perhaps?
Eugene But (OOO till 7-30)
2015/03/10 18:45:05
Will do in followup CL per offline discussion.
| |
| 46 } | |
| 47 | |
| 48 - (NSString*)cr_stringByElidingToFitSize:(CGSize)bounds { | |
| 49 CGSize sizeForGuess = CGSizeMake(bounds.width, CGFLOAT_MAX); | |
| 50 // Use binary search on the string's length. | |
| 51 size_t lo = 0; | |
| 52 size_t hi = [self length]; | |
| 53 size_t guess = 0; | |
| 54 for (guess = (lo + hi) / 2; lo < hi; guess = (lo + hi) / 2) { | |
| 55 NSString* tempString = [self cr_stringByCuttingToIndex:guess]; | |
| 56 UIFont* font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; | |
| 57 CGSize sizeGuess = | |
| 58 [tempString cr_boundingSizeWithSize:sizeForGuess font:font]; | |
| 59 if (sizeGuess.height > bounds.height) { | |
|
rohitrao (ping after 24h)
2015/03/10 18:35:53
Why do we compare heights here?
rohitrao (ping after 24h)
2015/03/10 18:37:20
Oh, I think this is testing how many lines are in
| |
| 60 hi = guess - 1; | |
| 61 if (hi < lo) | |
| 62 hi = lo; | |
| 63 } else { | |
| 64 lo = guess + 1; | |
| 65 } | |
| 66 } | |
| 67 return [self cr_stringByCuttingToIndex:lo]; | |
| 68 } | |
| 69 | |
| 40 @end | 70 @end |
| OLD | NEW |