Instance Method

draw(at:withAttributes:)

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

Declaration

iOS, tvOS, watchOS
func draw(at point: CGPoint, withAttributes attrs: [String : Any]? = nil)
macOS
func draw(at point: NSPoint, withAttributes attrs: [String : Any]? = nil)

Parameters

point

The point in the current graphics context where you want to start drawing the string. The coordinate system of the graphics context is usually defined by the view in which you are drawing. In AppKit, the origin is normally in the lower-left corner of the drawing area, but the origin is in the upper-left corner if the focused view is flipped.

attrs

A dictionary of text attributes to be applied to 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

The width (height for vertical layout) of the rendering area is unlimited, unlike draw(in:withAttributes:), which uses a bounding rectangle. As a result, this method renders the text in a single line. However, 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.