UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to let users choose a font with UIFontPickerViewController

Swift version: 5.6

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)

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

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 Pro Swift Buy Pro SwiftUI 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 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 Beyond Code

Was this page useful? Let us know!

Average rating: 4.4/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.