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

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

Issue 497503004: Add NSString category for providing iOS6-style string drawing APIs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: pixel-align size Created 6 years, 4 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
Index: ui/ios/NSString+CrStringDrawing.mm
diff --git a/ui/ios/NSString+CrStringDrawing.mm b/ui/ios/NSString+CrStringDrawing.mm
new file mode 100644
index 0000000000000000000000000000000000000000..f26cb5742bb3561035c5eb0d27af226e61ef945d
--- /dev/null
+++ b/ui/ios/NSString+CrStringDrawing.mm
@@ -0,0 +1,36 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ui/ios/NSString+CrStringDrawing.h"
+
+#include "base/logging.h"
+
+namespace {
+// Returns the closest pixel-aligned value higher than |value|, taking the scale
+// factor into account. At a scale of 1, equivalent to ceil().
+CGFloat alignValueToUpperPixel(CGFloat value) {
stuartmorgan 2014/08/29 14:48:31 Let's upstream the existing utility (with its test
lliabraa 2014/09/02 13:29:15 Done.
+ static CGFloat scale = [[UIScreen mainScreen] scale];
+ return ceil(value * scale) / scale;
+}
+} // namespace
+
+@implementation NSString (CrStringDrawing)
+
+- (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font {
+ DCHECK(font) << "|font| can not be nil; it is used as a NSDictionary value";
+ NSDictionary* attributes = @{ NSFontAttributeName : font };
+ CGSize size = [self sizeWithAttributes:attributes];
+ return CGSizeMake(alignValueToUpperPixel(size.width),
+ alignValueToUpperPixel(size.height));
+}
+
+- (CGSize)cr_sizeWithFont:(UIFont*)font {
+ if (!font)
+ return CGSizeZero;
+ NSDictionary* attributes = @{ NSFontAttributeName : font };
+ CGSize size = [self sizeWithAttributes:attributes];
+ return CGSizeMake(ceil(size.width), ceil(size.height));
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698