blob: 3c766ca09d6188ab1fbe6b059bcb55cf08bdda89 [file] [log] [blame]
lliabraac018ddc2014-09-03 20:36:551// 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
stuartmorgan018f5c022015-03-09 20:19:155#ifndef UI_GFX_IOS_NSSTRING_CR_STRING_DRAWING_H_
6#define UI_GFX_IOS_NSSTRING_CR_STRING_DRAWING_H_
lliabraac018ddc2014-09-03 20:36:557
8#import <UIKit/UIKit.h>
9
10@interface NSString (CrStringDrawing)
11
lliabraa49816642014-11-07 15:20:3212// Calculates and returns the bounding rect for the receiver drawn using the
13// given size and font.
14// This method is implemented as a wrapper around
15// |boundingRectWithSize:options:attributes:context:| using the following values
16// for the parameters:
17// - size: the provided |size|
18// - options: NSStringDrawingUsesLineFragmentOrigin
19// - attributes: a NSDictionary with the provided |font|
20// - context: nil.
21//
22// Note that the rect returned may contain fractional values.
23- (CGRect)cr_boundingRectWithSize:(CGSize)size
24 font:(UIFont*)font;
25
26// Convenience wrapper to just return the size of |boundingRectWithSize:font:|.
27//
28// Note that the size returned may contain fractional values.
29- (CGSize)cr_boundingSizeWithSize:(CGSize)size
30 font:(UIFont*)font;
31
lliabraac018ddc2014-09-03 20:36:5532// Returns the size of the string if it were to be rendered with the specified
33// font on a single line. The width and height of the CGSize returned are
34// pixel-aligned.
35//
36// This method is a convenience wrapper around sizeWithAttributes: to avoid
37// boilerplate required to put |font| in a dictionary of attributes. Do not pass
38// nil into this method.
39- (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font;
40
41// Deprecated: Use cr_pixelAlignedSizeWithFont: or sizeWithAttributes:
42// Provides a drop-in replacement for sizeWithFont:, which was deprecated in iOS
43// 7 in favor of -sizeWithAttributes:. Specifically, this method will return
44// CGSizeZero if |font| is nil, and the width and height returned are rounded up
45// to integer values.
46// TODO(lliabraa): This method was added to ease the transition off of the
47// deprecated sizeWithFont: method. New call sites should not be added and
48// existing call sites should be audited to determine the correct behavior for
49// nil |font| and rounding, then replaced with cr_pixelAlignedSizeWithFont: or
50// sizeWithAttributes: (crbug.com/364419).
51- (CGSize)cr_sizeWithFont:(UIFont*)font;
52
eugenebutbe0e83a2015-03-10 19:40:0153// If |index| is 0, returns an empty string.
54// If |index| is >= than self.length, returns self.
55// Otherwise, returns string cut to have |index| characters with an
56// ellipsis at the end.
57- (NSString*)cr_stringByCuttingToIndex:(NSUInteger)index;
58
59// Returns an elided version of string that fits in |bounds|.
60// System font of Label size is used for determining the string drawing size.
61- (NSString*)cr_stringByElidingToFitSize:(CGSize)bounds;
62
lliabraac018ddc2014-09-03 20:36:5563@end
64
stuartmorgan018f5c022015-03-09 20:19:1565#endif // UI_GFX_IOS_NSSTRING_CR_STRING_DRAWING_H_