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 // TODO(lliabraa): Move this method to a common util file (crbug.com/409823). | |
13 CGFloat alignValueToUpperPixel(CGFloat value) { | |
14 static CGFloat scale = [[UIScreen mainScreen] scale]; | |
Nico
2014/09/02 18:04:30
Is this an expensive method? If not, don't cache i
lliabraa
2014/09/03 18:02:33
Done.
| |
15 return ceil(value * scale) / scale; | |
16 } | |
17 } // namespace | |
18 | |
19 @implementation NSString (CrStringDrawing) | |
20 | |
21 - (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font { | |
22 DCHECK(font) << "|font| can not be nil; it is used as a NSDictionary value"; | |
23 NSDictionary* attributes = @{ NSFontAttributeName : font }; | |
24 CGSize size = [self sizeWithAttributes:attributes]; | |
25 return CGSizeMake(alignValueToUpperPixel(size.width), | |
26 alignValueToUpperPixel(size.height)); | |
27 } | |
28 | |
29 - (CGSize)cr_sizeWithFont:(UIFont*)font { | |
30 if (!font) | |
31 return CGSizeZero; | |
32 NSDictionary* attributes = @{ NSFontAttributeName : font }; | |
33 CGSize size = [self sizeWithAttributes:attributes]; | |
34 return CGSizeMake(ceil(size.width), ceil(size.height)); | |
35 } | |
36 | |
37 @end | |
OLD | NEW |