Chromium Code Reviews| 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 |