Instance Method

draw(in:withAttributes:)

Draws the attributed string inside the specified bounding rectangle.

Declaration

iOS, tvOS, watchOS
func draw(in rect: CGRect, withAttributes attrs: [String : Any]? = nil)
macOS
func draw(in rect: NSRect, withAttributes attrs: [String : Any]? = nil)

Parameters

rect

The bounding rectangle in which to draw the string. In AppKit, the origin of the bounding box is normally in the lower-left corner, but the origin is in the upper-left corner if the focused view is flipped.

attrs

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.

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 long to fit inside the rectangle, the method renders as much as possible and clips the rest.

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.

There must be either a focused view or an active graphics context when you call this method.