Swift version: 5.10
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)
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Available from iOS 13.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.