Instance Method


Returns the bounding rectangle required to draw the string.


iOS, Mac Catalyst, tvOS, watchOS
func boundingRect(with size: CGSize, options: NSStringDrawingOptions = [], context: NSStringDrawingContext?) -> CGRect
func boundingRect(with size: NSSize, options: NSString.DrawingOptions = [], context: NSStringDrawingContext?) -> NSRect



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


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


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.


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 usesLineFragmentOrigin in options.

See Also

Getting Metrics for the String

func size() -> CGSize

Returns the size required to draw the string.

func containsAttachments(in: NSRange) -> Bool

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