From 4981664e4c0407a7fb7443733f989991e3f883a6 Mon Sep 17 00:00:00 2001 From: lliabraa Date: Fri, 7 Nov 2014 07:20:32 -0800 Subject: [ui/ios] Add helper methods for getting the bounding size of some text. These helpers are meant to ease the transition from the deprecated |sizeWithFont:constrainedToSize:| methods. They'll also help reduce boilerplate compared to the iOS method |boundingRectWithSize:options:attributes:context:| BUG=364419 Review URL: https://codereview.chromium.org/704283002 Cr-Commit-Position: refs/heads/master@{#303237} --- ui/ios/NSString+CrStringDrawing.mm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'ui/ios/NSString+CrStringDrawing.mm') diff --git a/ui/ios/NSString+CrStringDrawing.mm b/ui/ios/NSString+CrStringDrawing.mm index 214b0c1..e9274e9 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 }; -- cgit v1.1