Contents

preferredFont(forTextStyle:options:)

Returns the font associated with the text style.

Declaration

class func preferredFont(forTextStyle style: NSFont.TextStyle, options: [NSFont.TextStyleOptionKey : Any] = [:]) -> NSFont

Parameters

  • style:

    The text style for which to return a font. See Textstyle for available values.

  • options:

    A dictionary you use to further configure the returned font. See Textstyleoptionkey for a list of valid keys. Pass an empty dictionary to use the default configuration.

Return Value

The font associated with the text style.

Discussion

Discussion

A font’s metrics, such as ascender, descender, and leading, can differ across devices for the same text style and point size. AppKit reserves extra vertical space to accommodate scripts like Thai and Hindi whenever someone includes one of those languages in their preferred languages, even if your text doesn’t use that script.

To take advantage of this behavior, create a font explicitly with this method and assign it to a text element. Don’t clip these text elements: ascenders and descenders for languages like Thai and Hindi often protrude beyond the expected line height. This typically isn’t a problem, since layouts usually leave extra space around neighboring elements, but clipping the view clips that text.

See Also

Creating System Fonts