Instance Method

draw(with:options:attributes:context:)

Draws the attributed string in the specified bounding rectangle using the provided options.

Declaration

iOS, tvOS, watchOS
func draw(with rect: CGRect, options: NSStringDrawingOptions = [], attributes: [NSAttributedStringKey : Any]? = nil, context: NSStringDrawingContext?)
macOS
func draw(with rect: NSRect, options: NSString.DrawingOptions = [], attributes: [NSAttributedStringKey : Any]? = nil, context: NSStringDrawingContext?)

Parameters

rect

The bounding rectangle in which to draw the string.

options

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

attributes

The text attributes with which to draw the string. These are the same attributes that can be applied to an NSAttributedString object, but in the case of NSString objects, the attributes apply to the entire string, rather than ranges within the string.

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.

Discussion

This method draws as much of the string as it can inside the specified rectangle, wrapping the string text as needed to make it fit. If the string is too big to fit completely inside the rectangle, the method scales the font or adjusts the letter spacing to make the string fit within the given bounds.

If newline characters are present in the string, those characters are honored and cause subsequent text to be placed on the next line underneath the starting point. To correctly draw and size multi-line text, pass usesLineFragmentOrigin in the options parameter.

Special Considerations

This method uses the baseline origin by default.

If usesLineFragmentOrigin is not specified, the rectangle’s height will be ignored and the operation considered to be single-line rendering.

See Also

Drawing Strings

func draw(at: CGPoint, withAttributes: [NSAttributedStringKey : Any]? = nil)

Draws the receiver with the font and other display characteristics of the given attributes, at the specified point in the current graphics context.

func draw(in: CGRect, withAttributes: [NSAttributedStringKey : Any]? = nil)

Draws the attributed string inside the specified bounding rectangle.

func boundingRect(with: CGSize, options: NSStringDrawingOptions = [], attributes: [NSAttributedStringKey : Any]? = nil, context: NSStringDrawingContext?)

Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.

func size(withAttributes: [NSAttributedStringKey : Any]? = nil)

Returns the bounding box size the receiver occupies when drawn with the given attributes.