Class

NSFontDescriptor

NSFontDescriptor objects provide a mechanism to describe a font with a dictionary of attributes. This font descriptor can be used later to create or modify an NSFont object. macOS 10.4 and later provides 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:).

Overview

There are several ways to create a new NSFontDescriptor object. You can 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 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.

Symbols

Creating a Font Descriptor

init(name: String, matrix: AffineTransform)

Returns a font descriptor with the NSFontNameAttribute and NSFontMatrixAttribute dictionary attributes set to the given values.

init(name: String, size: CGFloat)

Returns a font descriptor with the NSFontNameAttribute and NSFontSizeAttribute dictionary attributes set to the given values.

func addingAttributes([String : 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(AffineTransform)

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(NSFontSymbolicTraits)

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: [String : Any]? = nil)

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

Finding Fonts

func matchingFontDescriptors(withMandatoryKeys: Set<String>?)

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

func matchingFontDescriptor(withMandatoryKeys: Set<String>?)

Returns a normalized font descriptor whose specified attributes match those of the receiver.

Querying a Font Descriptor

var fontAttributes: [String : Any]

The receiver’s dictionary of attributes.

var matrix: AffineTransform?

The current transform matrix of the receiver.

func object(forKey: String)

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: NSFontSymbolicTraits

A bit mask that describes the traits of the receiver.

Constants

Font Attributes

These font attributes are defined by NSFontDescriptor.

Font Traits Dictionary Keys

The following constants can be used as keys to retrieve information about a font descriptor from its trait dictionary.

Font Variation Axis Dictionary Keys

The following constants can be used as keys to retrieve information about a font descriptor from its variation axis dictionary.

Font Feature Keys

The following constants can be used as keys to retrieve information about a font descriptor from its feature dictionary.

NSFontSymbolicTraits

NSFontSymbolicTraits symbolically describes stylistic aspects of a font.

NSFontFamilyClass

These constants classify certain stylistic qualities of the font. These values correspond closely to the font class values in the OpenType OS/2 table. The class values are bundled in the upper four bits of the NSFontSymbolicTraits and can be accessed via NSFontFamilyClassMask. For more information about the specific meaning of each identifier, refer to the OpenType specification.

NSFontFamilyClassMask

This constant is used to access NSFontFamilyClass values in the upper four bits of NSFontSymbolicTraits.

Typeface Information

Typeface information is specified by the lower 16 bits of NSFontSymbolicTraits using the following constants.

Relationships

Inherits From