Index: ui/gfx/ios/NSString+CrStringDrawing.mm |
diff --git a/ui/gfx/ios/NSString+CrStringDrawing.mm b/ui/gfx/ios/NSString+CrStringDrawing.mm |
index 0d6d0b4c1d9deca9a01b8f54962bf96f813d29af..cc9f799b7997e22466b551027f97ee49657cecfc 100644 |
--- a/ui/gfx/ios/NSString+CrStringDrawing.mm |
+++ b/ui/gfx/ios/NSString+CrStringDrawing.mm |
@@ -37,4 +37,34 @@ |
return CGSizeMake(ceil(size.width), ceil(size.height)); |
} |
+- (NSString*)cr_stringByCuttingToIndex:(NSUInteger)index { |
+ if (index == 0) |
+ return @""; |
+ if (index >= [self length]) |
+ return [[self retain] autorelease]; |
+ 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.
|
+} |
+ |
+- (NSString*)cr_stringByElidingToFitSize:(CGSize)bounds { |
+ CGSize sizeForGuess = CGSizeMake(bounds.width, CGFLOAT_MAX); |
+ // Use binary search on the string's length. |
+ size_t lo = 0; |
+ size_t hi = [self length]; |
+ size_t guess = 0; |
+ for (guess = (lo + hi) / 2; lo < hi; guess = (lo + hi) / 2) { |
+ NSString* tempString = [self cr_stringByCuttingToIndex:guess]; |
+ UIFont* font = [UIFont systemFontOfSize:[UIFont labelFontSize]]; |
+ CGSize sizeGuess = |
+ [tempString cr_boundingSizeWithSize:sizeForGuess font:font]; |
+ 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
|
+ hi = guess - 1; |
+ if (hi < lo) |
+ hi = lo; |
+ } else { |
+ lo = guess + 1; |
+ } |
+ } |
+ return [self cr_stringByCuttingToIndex:lo]; |
+} |
+ |
@end |