Instance Method

size(withAttributes:)

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

Declaration

iOS, tvOS, watchOS
func size(withAttributes attrs: [NSAttributedStringKey : Any]? = nil) -> CGSize
macOS
func size(withAttributes attrs: [NSAttributedStringKey : Any]? = nil) -> NSSize

Parameters

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.

Return Value

The bounding box size the receiver occupies when drawn with the specified attributes.

Discussion

This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function.

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.