Class

UIFont

The interface for getting and setting font information.

Overview

UIFont provides you with access to the font’s characteristics and also provides the system with access to the font’s glyph information, which is used during layout. You use font objects by passing them to methods that accept them as a parameter.

You do not create font objects using the alloc and init methods. Instead, you use class methods of UIFont, such as preferredFont(forTextStyle:), to look up and retrieve the desired font object. These methods check for an existing font object with the specified characteristics and return it if it exists. Otherwise, they create a new font object based on the desired font characteristics.

Font objects are immutable and so it is safe to use them from multiple threads in your app.

Topics

Creating Fonts

class func preferredFont(forTextStyle: UIFontTextStyle)

Returns an instance of the system font for the specified text style and scaled appropriately for the user's selected content size category.

class func preferredFont(forTextStyle: UIFontTextStyle, compatibleWith: UITraitCollection?)

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

init?(name: String, size: CGFloat)

Creates and returns a font object for the specified font name and size.

init(descriptor: UIFontDescriptor, size: CGFloat)

Returns a font matching the given font descriptor.

func withSize(CGFloat)

Returns a font object that is the same as the receiver but which has the specified size instead.

Creating System Fonts

class func systemFont(ofSize: CGFloat)

Returns the font object used for standard interface items in the specified size.

class func systemFont(ofSize: CGFloat, weight: UIFont.Weight)

Returns the font object used for standard interface items in the specified size and weight.

class func boldSystemFont(ofSize: CGFloat)

Returns the font object used for standard interface items that are rendered in boldface type in the specified size.

class func italicSystemFont(ofSize: CGFloat)

Returns the font object used for standard interface items that are rendered in italic type in the specified size.

class func monospacedDigitSystemFont(ofSize: CGFloat, weight: UIFont.Weight)

Returns the font object used for standard interface items that require fixed distances between digits.

Getting the Available Font Names

class var familyNames: [String]

Returns an array of font family names available on the system.

class func fontNames(forFamilyName: String)

Returns an array of font names available in a particular font family.

Getting Font Name Attributes

var familyName: String

The font family name.

var fontName: String

The font face name.

Getting Font Metrics

var pointSize: CGFloat

The receiver’s point size, or the effective vertical point size for a font with a nonstandard matrix.

var ascender: CGFloat

The top y-coordinate, offset from the baseline, of the receiver’s longest ascender.

var descender: CGFloat

The bottom y-coordinate, offset from the baseline, of the receiver’s longest descender.

var leading: CGFloat

The receiver’s leading information.

var capHeight: CGFloat

The receiver’s cap height information.

var xHeight: CGFloat

The x-height of the receiver.

var lineHeight: CGFloat

The height of text lines (measured in points).

Getting System Font Information

class var labelFontSize: CGFloat

Returns the standard font size used for labels.

class var buttonFontSize: CGFloat

Returns the standard font size used for buttons.

class var smallSystemFontSize: CGFloat

Returns the size of the standard small system font.

class var systemFontSize: CGFloat

Returns the size of the standard system font.

Getting Font Descriptors

var fontDescriptor: UIFontDescriptor

Returns a font descriptor that describes the font.

Relationships

Inherits From

See Also

Fonts

class UIFontDescriptor

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

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.