SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

How to let users choose a font with UIFontPickerViewController

Swift version: 5.1

Paul Hudson    @twostraws   

UIKit provides UIFontPickerViewController as a built-in view controller for letting users select from a list of installed fonts available for our apps. Using it takes three steps: create a delegate to handle callbacks, create and show an instance of the font picker, then read the response as appropriate.

As an example, if you had a UIViewController subclass that wanted to show a font picker, you would make it conform to the UIFontPickerViewControllerDelegate protocol like this:

class ViewController: UIViewController, UIFontPickerViewControllerDelegate {
    // the rest of your class
}

Second, you would create the font picker, assign the current view controller as its delegate, then show it like this:

let vc = UIFontPickerViewController()
vc.delegate = self
present(vc, animated: true)

Finally, you would implement the fontPickerViewControllerDidPickFont() method. This sends you back the UIFontPickerViewController instance you created, from which you can read the font descriptor that was chosen.

If you weren’t already aware, a font descriptor is different from a font: it describes the type of font chosen, but doesn’t associate a size with it. So, if you want to use the selected font in a label you need to create a UIFont instance from it.

For example, you might write this:

func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
    // attempt to read the selected font descriptor, but exit quietly if that fails
    guard let descriptor = viewController.selectedFontDescriptor else { return }

    let font = UIFont(descriptor: descriptor, size: 36)
    yourLabel.font = font
}

You don’t need to dismiss the font picker; it will be dismissed automatically.

If you want to, you can optionally also add the fontPickerViewControllerDidCancel() method, which will be called if the user cancels the font picker rather than selecting a font:

func fontPickerViewControllerDidCancel(_ viewController: UIFontPickerViewController) {
    // handle cancel event here
}

Again, this will automatically dismiss the font picker for you, so you don’t need to do it yourself.

It’s worth adding that you have some control over how the font picker works. More specifically, you can create it with a customization class that contains three useful properties:

  • displayUsingSystemFont will show each font in the default system font, rather than using the font itself. This sacrifices some usefulness for legibility. (This is false by default.)
  • includeFaces adds a dropdown arrow next to each font type, letting users select different weights and options. (This is also false by default.)
  • filteredTraits is an array of traits that limit the types of font you want to show. (This is empty by default, so all fonts are shown.)

For example, if we wanted to show a font picker in system fonts, with faces included, but only showing serif fonts (think Times New Roman rather than Helvetica), we’d write code like this:

let configuration = UIFontPickerViewController.Configuration()
configuration.includeFaces = true
configuration.displayUsingSystemFont = true
configuration.filteredTraits = [.classModernSerifs]

let vc = UIFontPickerViewController(configuration: configuration)

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

Available from iOS 13.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5