Contents

preferredFont(forTextStyle:compatibleWith:)

Returns an instance of the system font for the appropriate text style and traits.

Declaration

class func preferredFont(forTextStyle style: UIFont.TextStyle, compatibleWith traitCollection: UITraitCollection?) -> UIFont

Parameters

  • style:

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

  • traitCollection:

    The traits to use when determining which font to return.

Return Value

The system font associated with the specified text style and traits.

Discussion

Discussion

To create a styled font based on a custom font, use a UIFontMetrics object.

Because fonts are immutable, any element that adjusts for an updated content size category does not modify the font itself. Instead, the element replaces the assigned font with a new instance based on the original settings.

A font’s metrics, such as ascender, descender, and lineHeight, can differ across devices for the same text style and point size. UIKit 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, like a UILabel. Don’t set clipsToBounds on these text elements: ascenders and descenders for languages like Thai and Hindi often protrude beyond the line height bounds. This typically isn’t a problem, since layouts usually leave extra space around neighboring elements, but setting clipsToBounds clips that text. UIKit no longer enables clipsToBounds by default in places where it previously did.

See Also

Creating Fonts