UIFont
An object that provides access to the font’s characteristics.
Declaration
class UIFontMentioned in
Overview
Use UIFont to access your font’s characteristics within your app. The system also uses it to access glyph information during layout. Font objects are immutable, so it’s safe to use them from multiple threads in your app.
In Objective-C, don’t create font objects using the alloc and init methods. Instead, 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.
Understanding font metrics
Rely on a font’s metrics to understand how its glyphs occupy vertical space relative to the baseline, a shared reference line that text aligns to. Check a font’s pointSize to find the nominal size you requested when creating it, and use its ascender and descender properties to measure the ascent (the space above the baseline that the font reserves for its tallest glyphs) and descent (the space below the baseline that the font reserves for its lowest-reaching glyphs), as the first diagram shows. Use leading and lineHeight to find the extra space a font recommends between lines of text and the total vertical space a single line of text occupies, as the second diagram shows.
Determine the height of individual glyphs relative to the baseline using metrics like capHeight and xHeight, as the following diagram shows.
[Image]
Combine these metrics to calculate the size of a block of text without rendering it, which helps you lay out custom text views or align text with other content.
Topics
Creating Fonts
Scaling fonts automaticallyCreating self-sizing table view cellspreferredFont(forTextStyle:)preferredFont(forTextStyle:compatibleWith:)UIFont.TextStyleinit(name:size:)init(descriptor:size:)withSize(_:)
Creating System Fonts
systemFont(ofSize:)systemFont(ofSize:weight:)UIFont.WeightsystemFont(ofSize:weight:width:)UIFont.WidthboldSystemFont(ofSize:)italicSystemFont(ofSize:)monospacedSystemFont(ofSize:weight:)monospacedDigitSystemFont(ofSize:weight:)