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

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: don't cache scale Created 6 years, 3 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/ios/NSString+CrStringDrawing.h ('k') | ui/ios/NSString+CrStringDrawing_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..9f7d88b2684f221dd054f86844a44e5b3a6cedd8
--- /dev/null
+++ b/ui/ios/NSString+CrStringDrawing.mm
@@ -0,0 +1,37 @@
+// 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().
+// TODO(lliabraa): Move this method to a common util file (crbug.com/409823).
+CGFloat alignValueToUpperPixel(CGFloat value) {
+ 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
« no previous file with comments | « ui/ios/NSString+CrStringDrawing.h ('k') | ui/ios/NSString+CrStringDrawing_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698