Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(530)

Unified Diff: ui/gfx/ios/NSString+CrStringDrawing.mm

Issue 974913006: Upstream NSString+CrStringDrawing changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merged with origin/master Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/ios/NSString+CrStringDrawing.h ('k') | ui/gfx/ios/NSString+CrStringDrawing_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « ui/gfx/ios/NSString+CrStringDrawing.h ('k') | ui/gfx/ios/NSString+CrStringDrawing_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698