NEW: Join my free 100 Days of SwiftUI challenge today! >>

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)

LEARN SWIFTUI FOR FREE I have a massive, free SwiftUI video collection on YouTube teaching you how to build complete apps with SwiftUI – check it out!

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.

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5