Class

UIFontDescriptor

A mechanism to describe a font with a dictionary of attributes.

Overview

A font descriptor can be used later to create or modify a UIFont object. Font descriptors can be archived and unarchived. Font descriptors have a font matching capability, so that you can partially describe a font by creating a font descriptor with, for example, just a family name. You can then find all the available fonts on the system with a matching family name using matchingFontDescriptors(withMandatoryKeys:).

There are several ways to create a new UIFontDescriptor object. To take advantage of text styles and respect the user’s current content size category, use preferredFontDescriptor(withTextStyle:). You can also use alloc and init(fontAttributes:), fontDescriptorWithFontAttributes:, init(name:matrix:), or init(name:size:) to create a font descriptor based on either your custom attributes dictionary or on a specific font’s name and size. Alternatively you can use one of the fontDescriptor… instance methods (such as withFace(_:)) to create a modified version of an existing descriptor (the receiver). The latter methods are useful if you have an existing descriptor and simply want to change one aspect.

All attributes in the attributes dictionary are optional.

Topics

Creating a Font Descriptor

class func preferredFontDescriptor(withTextStyle: UIFontTextStyle)

Returns a font descriptor containing the given text style and the user's selected content size category.

class func preferredFontDescriptor(withTextStyle: UIFontTextStyle, compatibleWith: UITraitCollection?)

Returns a font descriptor containing the text style and the content size category specified by the provided trait collection.

init(name: String, matrix: CGAffineTransform)

Returns a font descriptor with the UIFontDescriptorNameAttribute and UIFontDescriptorMatrixAttribute dictionary attributes set to the given values.

init(name: String, size: CGFloat)

Returns a font descriptor with the UIFontDescriptorNameAttribute and UIFontDescriptorSizeAttribute dictionary attributes set to the given values.

func addingAttributes([UIFontDescriptor.AttributeName : Any] = [:])

Returns a new font descriptor that is the same as the receiver but with the specified attributes taking precedence over the existing ones.

func withFace(String)

Returns a new font descriptor that is the same as the receiver but with the specified face.

func withFamily(String)

Returns a new font descriptor whose attributes are the same as the receiver but from the specified family.

func withMatrix(CGAffineTransform)

Returns a new font descriptor that is the same as the receiver but with the specified matrix.

func withSize(CGFloat)

Returns a new font descriptor that is the same as the receiver but with the specified point size.

func withSymbolicTraits(UIFontDescriptorSymbolicTraits)

Returns a new font descriptor that is the same as the receiver but with the specified symbolic traits taking precedence over the existing ones.

Initializing a Font Descriptor

init(fontAttributes: [UIFontDescriptor.AttributeName : Any] = [:])

Initializes and returns a new font descriptor with the specified attributes.

Finding Fonts

func matchingFontDescriptors(withMandatoryKeys: Set<UIFontDescriptor.AttributeName>?)

Returns all the fonts available on the system whose specified attributes match those of the receiver.

Querying a Font Descriptor

var fontAttributes: [UIFontDescriptor.AttributeName : Any]

Returns the receiver’s dictionary of attributes.

var matrix: CGAffineTransform

The current transform matrix of the receiver.

func object(forKey: UIFontDescriptor.AttributeName)

Returns the font attribute specified by the given key.

var pointSize: CGFloat

The point size of the receiver.

var postscriptName: String

The PostScript name of the receiver.

var symbolicTraits: UIFontDescriptorSymbolicTraits

A bit mask that describes the traits of the receiver.

Constants

struct UIFontTextStyle

Constants that describe the preferred styles to use for fonts.

struct UIFontDescriptorSymbolicTraits

A symbolic description of the stylistic aspects of a font.

typealias UIFontDescriptorClass

UIFontDescriptorClass classifies certain stylistic qualities of the font.

struct UIFontDescriptor.AttributeName

These font attributes are defined by UIFontDescriptor.

struct UIFontDescriptor.FeatureKey

Constants that you use to retrieve feature settings.

struct UIFontDescriptor.TraitKey

Keys that you use to retrieve the font descriptor's trait information.

Font Weights

The following constants represent font weights that can be used as values for weight.

Relationships

Inherits From

See Also

Fonts

Getting a Scaled Font

Scale text in your interface automatically by creating fonts that support Dynamic Type.

Adding a Custom Font to Your App

Add a custom font to your app and use it in your app’s interface.

class UIFont

The interface for getting and setting font information.

struct UIFontDescriptorSymbolicTraits

A symbolic description of the stylistic aspects of a font.

protocol UIContentSizeCategoryAdjusting

A collection of methods that give controls an easy way to adopt automatic adjustment to content category changes.

class UIFontMetrics

A utility object for obtaining custom fonts that scale to support Dynamic Type.