diff options
author | Torne (Richard Coles) <torne@google.com> | 2014-11-12 17:59:43 +0000 |
---|---|---|
committer | Torne (Richard Coles) <torne@google.com> | 2014-11-12 17:59:43 +0000 |
commit | 1850ca92fc5c5faa2907b3befcf40067265148cc (patch) | |
tree | 7d10585f25356b3ee63b5ab4255161fe27725ee4 /ui/ios | |
parent | e9e1f6521e4fef99aa1a1928c70ef3dfb55a8d9e (diff) | |
download | chromium_org-1850ca92fc5c5faa2907b3befcf40067265148cc.tar.gz |
Merge from Chromium at DEPS revision 03655fd3f6d7
This commit was generated by merge_to_master.py.
Change-Id: Ifba5396691b9164ba027be04398f7bc8e938750d
Diffstat (limited to 'ui/ios')
-rw-r--r-- | ui/ios/NSString+CrStringDrawing.h | 20 | ||||
-rw-r--r-- | ui/ios/NSString+CrStringDrawing.mm | 14 | ||||
-rw-r--r-- | ui/ios/NSString+CrStringDrawing_unittest.mm | 48 |
3 files changed, 80 insertions, 2 deletions
diff --git a/ui/ios/NSString+CrStringDrawing.h b/ui/ios/NSString+CrStringDrawing.h index 330593ea9d..0a32a235a9 100644 --- a/ui/ios/NSString+CrStringDrawing.h +++ b/ui/ios/NSString+CrStringDrawing.h @@ -9,6 +9,26 @@ @interface NSString (CrStringDrawing) +// Calculates and returns the bounding rect for the receiver drawn using the +// given size and font. +// This method is implemented as a wrapper around +// |boundingRectWithSize:options:attributes:context:| using the following values +// for the parameters: +// - size: the provided |size| +// - options: NSStringDrawingUsesLineFragmentOrigin +// - attributes: a NSDictionary with the provided |font| +// - context: nil. +// +// Note that the rect returned may contain fractional values. +- (CGRect)cr_boundingRectWithSize:(CGSize)size + font:(UIFont*)font; + +// Convenience wrapper to just return the size of |boundingRectWithSize:font:|. +// +// Note that the size returned may contain fractional values. +- (CGSize)cr_boundingSizeWithSize:(CGSize)size + font:(UIFont*)font; + // Returns the size of the string if it were to be rendered with the specified // font on a single line. The width and height of the CGSize returned are // pixel-aligned. diff --git a/ui/ios/NSString+CrStringDrawing.mm b/ui/ios/NSString+CrStringDrawing.mm index 214b0c1910..e9274e9849 100644 --- a/ui/ios/NSString+CrStringDrawing.mm +++ b/ui/ios/NSString+CrStringDrawing.mm @@ -9,6 +9,20 @@ @implementation NSString (CrStringDrawing) +- (CGRect)cr_boundingRectWithSize:(CGSize)size + font:(UIFont*)font { + NSDictionary* attributes = font ? @{NSFontAttributeName: font} : @{}; + return [self boundingRectWithSize:size + options:NSStringDrawingUsesLineFragmentOrigin + attributes:attributes + context:nil]; +} + +- (CGSize)cr_boundingSizeWithSize:(CGSize)size + font:(UIFont*)font { + return [self cr_boundingRectWithSize:size font:font].size; +} + - (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font { DCHECK(font) << "|font| can not be nil; it is used as a NSDictionary value"; NSDictionary* attributes = @{ NSFontAttributeName : font }; diff --git a/ui/ios/NSString+CrStringDrawing_unittest.mm b/ui/ios/NSString+CrStringDrawing_unittest.mm index 6f0497d064..157d0214dd 100644 --- a/ui/ios/NSString+CrStringDrawing_unittest.mm +++ b/ui/ios/NSString+CrStringDrawing_unittest.mm @@ -13,11 +13,56 @@ namespace { typedef PlatformTest NSStringCrStringDrawing; -// These test verifies that the category methods return the same values as the +// These tests verify that the category methods return the same values as the // deprecated methods, so ignore warnings about using deprecated methods. #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" +// Verifies that |cr_boundingSizeWithSize| returns the same size as the +// deprecated |sizeWithFont:constrainedToSize| for most values. +// Note that the methods return different values in a few cases (so they are not +// included in the test cases): +// - the constrained size.width is less than a character. +// - the constrained size.height is less than the font height. +// - the string is empty. +TEST_F(NSStringCrStringDrawing, BoundingSizeWithSize) { + NSArray* fonts = @[ + [UIFont systemFontOfSize:16], + [UIFont boldSystemFontOfSize:10], + [UIFont fontWithName:@"Helvetica" size:12.0], + ]; + NSArray* strings = @[ + @"Test", + @"multi word test", + @"你好", + @"★ This is a test string that is very long.", + ]; + NSArray* sizes = @[ + [NSValue valueWithCGSize:CGSizeMake(20, 100)], + [NSValue valueWithCGSize:CGSizeMake(100, 100)], + [NSValue valueWithCGSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)], + ]; + for (UIFont* font in fonts) { + for (NSString* string in strings) { + for (NSValue* sizeValue in sizes) { + CGSize test_size = [sizeValue CGSizeValue]; + std::string test_tag = base::StringPrintf( + "for string '%s' with font %s and size %s", + base::SysNSStringToUTF8(string).c_str(), + base::SysNSStringToUTF8([font description]).c_str(), + base::SysNSStringToUTF8(NSStringFromCGSize(test_size)).c_str()); + + CGSize size_with_font = + [string sizeWithFont:font constrainedToSize:test_size]; + CGSize bounding_size = + [string cr_boundingSizeWithSize:test_size font:font]; + EXPECT_EQ(size_with_font.width, bounding_size.width) << test_tag; + EXPECT_EQ(size_with_font.height, bounding_size.height) << test_tag; + } + } + } +} + TEST_F(NSStringCrStringDrawing, SizeWithFont) { NSArray* fonts = @[ [NSNull null], @@ -103,5 +148,4 @@ TEST_F(NSStringCrStringDrawing, PixelAlignedSizeWithFont) { } } - } // namespace |