Instance Method

boundingRectWithSize:options:context:

Returns the bounding rectangle required to draw the string.

Declaration

iOS, Mac Catalyst, tvOS, watchOS
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context;
macOS
- (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context;

Parameters

size

The width and height constraints to apply when computing the string’s bounding rectangle.

options

Additional drawing options to apply to the string during rendering. For a list of possible values, see NSStringDrawingOptions.

context

A context object with information about how to adjust the font tracking and scaling information. On return, the specified object contains information about the actual values used to render the string. This parameter may be nil.

Return Value

A rectangle whose size component indicates the width and height required to draw the entire contents of the string.

Discussion

You can use this method to compute the space required to draw the string. The constraints you specify in the size parameter are a guide for the renderer for how to size the string. However, the actual bounding rectangle returned by this method can be larger than the constraints if additional space is needed to render the entire string. Typically, the renderer preserves the width constraint and adjusts the height constraint as needed.

In iOS 7 and later, this method returns fractional sizes (in the size component of the returned rectangle); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.

Special Considerations

To calculate the bounding rectangle, this method uses the baseline origin by default, so it behaves as a single line. To render the string in multiple lines, specify NSStringDrawingUsesLineFragmentOrigin in options.

See Also

Getting Metrics for the String

- size

Returns the size required to draw the string.

- containsAttachmentsInRange:

Returns a Boolean value that indicates if the attributed string contains a property configured in the specified range.