I'm a big believer in using dynamic type as much as possible. However, the API for dynamic type preferredFont(forTextStyle style: UIFontTextStyle) is a class method on UIFont and only returns the system text (SF) font. This is not so helpful when using custom fonts in your app.
I'd like to be able to provide dynamic type in whatever font my designers give me. And because I'm a Swift developer and am physically incapable of designing a solution that doesn't involve protocols I'd like my solution to conform to this protocol:
The protocol defines two methods that mirror the UIFont class methods for creating font objects.
I'd also like to avoid the dangers that come with typing out the names of fonts repeatedly. There's already a well defined solution to this, using enums to represent the strings. We'll make the enum conform to the CustomDynamicTypeable protocol.
Here we have an enum that contains the names of two fonts from the Judson family. Because this enum stores raw string values, create a protocol extension on CustomDynamicTypeable constrained to raw strings.
The implementation for the preferred font uses the standard UIFont class method to get the system font for the preferred text style and uses that font's point size to create a custom font of the same size.
Now that we have a default implementation for raw strings, we can use the enums directly to get our custom fonts for the text style we want
No fiddling with font names, descriptors or any of that stuff. And nothing is hard coded to specific font families. If you use multiple font families in your app just create enums for each font family, make them raw strings and conform to CustomDynamicTypeable.
I'm sure there are other (better) ways to do this, if there are, let me know in the comments.