The styleguide
SwiftPackage Font SwiftUI iOS Estimated reading time: 7 minutesStyle - this is one of the main aspects that make u’r app looking and feeling good. U can style the same app in different ways, using different techniques, but u always use the same components - fonts, colors, and images.
Every time this can be a bit annoying process - some typos in name of the image, or searching the source image in the .xcasset
catalog, managing fonts, by typing same and same name…
In this article I would like to cover how I handle this in my current project:
- Fonts
- Colors
- Images
Font
To add a custom Font in the application u should modify Info.plist
file:
After adding the font file to your project, you need to let iOS know about the font. To do this, add the key “Fonts provided by application” to
Info.plist
(the raw key name is UIAppFonts). Xcode creates an array value for the key; add the name of the font file as an item of the array. Be sure to include the file extension as part of the name.
Imagine, that u have one codebase for styling and few targets, a few Info.plist
. In case of any change - u need to not only modify the convenient methods for this Font usage, but also all plists.
Repeating operation almost always leads to some issues… What, if we can do these only ones.
Thanks to swift package, we can easily wrap all the logic in one place. Now, we also can add resources to it (thanks to one of the latest additions). So, in theory - any font can now be shared.
Actually, it’s possible to include resources into an earlier version of SP - u can use blob data, or create separate
.bundle
with resources, as I did for one of the projects.
The last problem to solve is - how to register fonts for each target? One way is already known (add special key-value into .plist
).
Another alternative can be a pre-run script that modifies Info.plist
, but this way is error-prone, and I would like to avoid it.
The best way - is to use CoreText
. Thanks to API from this framework, we can do font registration during app execution!
the original place, where I found this info - was an old blog post, available here
There is a few steps, that need to be done:
Step 1
Add font to Fonts
(or any name u like) directory (under a directory with the name of the SP)
Step 2
Modify the Package.swift
file, to include this directory with fonts:
the name of my package -
Styleguide
Step 3
Add code, that can allow registering the font. This is a bit tricky part - the CoreText
API allows us to register concrete font, so we need to somehow know which fonts we would like to register.
The good point here is that in SP we have a bundle with resources, so we can easily lookup resource file lists and select fonts only.
To do so, we can use the next code:
Add some entity, that represents Font
(we still use OOP ;])
Then, using FileManager
, we can search for available resources:
And the final step - registering the font:
u may suggest using
guard
instead ofif let
, or put every unwrap check into one check using,
or&&
operator, but I personally like this style and separate check - this can inform the concrete problem and, as for me, a bit more visualize the flow.
This code should be called somewhere like next:
Step 4
Register fonts in u’r target. This is the easiest part:
Benefits - 1 line of code, single source, no Info.plist
modification required!
To make things, even more, better, we can wrap usage of the font into some strongly-typed config:
Then, we can use specially designed ViewModifier
, that wraps all work required for custom font use:
Colors
To handle color, we agreed to use a single pallet of colors, and reuse them all across the app.
So with colors - everything is pretty simple:
I also saw a very interesting approach to handling colors in an open-source app isowords from pointfree.co:
This can be used to easily refer to color or to convert colors depending on the colorScheme
of the app.
Icons
In one of the previous projects, we created a wrapper for each image we can use in the app. The main purpose was to reduce the usage of strings and so, unmanaged resources.
And then, whenever u create a feature, just add specialized .xcassets
folder or folder inside single .xcassets
(as u wish) and extension to Icon
:
As result - usage of all images will be strongly typed and easily controlled.
If u like to use a lot SFSymbols, u may also want to check this repo
Resources
Share on: