OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ui/ios/NSString+CrStringDrawing.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace { | |
10 // Returns the closest pixel-aligned value higher than |value|, taking the scale | |
11 // factor into account. At a scale of 1, equivalent to ceil(). | |
12 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.
| |
13 static CGFloat scale = [[UIScreen mainScreen] scale]; | |
14 return ceil(value * scale) / scale; | |
15 } | |
16 } // namespace | |
17 | |
18 @implementation NSString (CrStringDrawing) | |
19 | |
20 - (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font { | |
21 DCHECK(font) << "|font| can not be nil; it is used as a NSDictionary value"; | |
22 NSDictionary* attributes = @{ NSFontAttributeName : font }; | |
23 CGSize size = [self sizeWithAttributes:attributes]; | |
24 return CGSizeMake(alignValueToUpperPixel(size.width), | |
25 alignValueToUpperPixel(size.height)); | |
26 } | |
27 | |
28 - (CGSize)cr_sizeWithFont:(UIFont*)font { | |
29 if (!font) | |
30 return CGSizeZero; | |
31 NSDictionary* attributes = @{ NSFontAttributeName : font }; | |
32 CGSize size = [self sizeWithAttributes:attributes]; | |
33 return CGSizeMake(ceil(size.width), ceil(size.height)); | |
34 } | |
35 | |
36 @end | |
OLD | NEW |