In this new series on Swift code topics, the steps to create a custom symbol using an exported SF Symbol as a starting template are described. Sample SwiftUI code is included for displaying and dynamically modifying the size, weight and color of standard SF Symbols and compatible custom symbols.
The concept, app and Xcode support for SF Symbols were introduced at WWDC 2019 along with a very large set of changes and updates to nearly everything else in the Xcode development world. WWDC 2019 was amazing in every aspect from a developer’s perspective and Apple’s “blown brain” logo was completely appropriate.
SF Symbols is a set of over 1500 vector-based symbol images (icons and glyphs) that can be used as icons during app development within Xcode. Each symbol image is defined by three scales (small, medium, large) and nine weights (Ultralight, Light, Regular, Medium, Semibold, Bold, Heavy, Black) that correspond to available scales and weights for the San Francisco system fonts. As such, SwiftUI code for SF Symbols can be integrated with surrounding text to keep size, weight, alignment and color consistent.
The SF Symbol App:
Using Apple’s SF Symbols App for Mac OS, it is easy to browse through the 1500+ SF Symbols and identify a symbol name to use in Xcode. The following SwiftUI code shows an example of including a SF Symbol (e.g., calendar) in a SwiftUI view within Xcode:
In this example code, SwiftUI modifiers are used to select the scale, weight, and color for the symbol.
Creating a custom symbol:
Even with 1500+ symbols, being able to design custom symbols is still needed. Several steps are necessary to create a custom symbol that is consistent with the system provided SF Symbols with respect to scales, weights, and alignment.
Any symbol in the SF Symbols app can be exported as an SVG file. If the exported SVG file is then imported into a vector graphics app, such as Affinity Designer in my case, then all 27 scale and weight combinations for the symbol (e.g. calendar) can be seen in the screenshot from Affinity Designer below:
To highlight the steps in creating a custom symbol, a calendar SF Symbol will be exported and used as a template to create a calculator symbol in Affinity Designer (ref: What’s in your toolbox?) Here are the steps:
Use the SF Symbols App to select a symbol to export.
After launching the SF Symbols app, I found the calendar symbol and selected it. The export command is located under the File menu (File -> Export Symbol) or via shortcut key (CMD-E) and results in an exported SVG file (ref: an XML-based vector image format file for 2-dimensional graphics) file.
Learn the SVG format for a Symbol file
Since SVG is an XML-based file, it can be opened in a text editor to inspect its contents and formats. For the custom calculator symbol to be compatible for importing into Xcode’s asset catalog, its SVG file format must follow a set of custom symbol guidelines established by Apple. Each SVG file is divided into three main grouped layers: Symbols, Guides, and Notes. This top-level file format for the calendar.svg file is shown below (detailed sublayers have been omitted for readability):
Expanding into the Symbols group, all 27 scale/weight variants of the calendar symbol are listed (detailed sublayers have been omitted for readability). Note that each variant is bracketed in a group.
Continuing to drill down, the definition of the Regular-M variant of the calendar symbol is shown.
Import Symbol file into Affinity Designer and Modify
Here are the steps to modify the symbol variants in Affinity Designer and maintain compliance to the Xcode file format:
Zoom into one of the variants (e.g., Regular-M)
Use two-finger click to display the context menu. Select Geometry -> Separate Curves. This will create a list of curves for each object (in this specific case with the calendar symbol, 12 objects consisting of a outer rounded rectangle, inner rounded rectangle (i.e., monthly cutout) and 10 individual rounded rectangles (i.e., days).
If these objects are recombined using Geometry -> Combine Curves, the exported SVG file from Affinity Designer will not be Xcode compliant. As shown in the format below, the Regular-M variant of the image is missing the group wrapper.
If we also group the Regular-M variant of the image in Affinity Designer, then the exported SVG file from Affinity Designer is now Xcode compliant. As shown in the format below, the Regular-M variant of the image includes the group wrapper. Note that the transformation matrix is missing, but the relative offsets have been adjusted for all parameters in each path.
At this point, we have the recipe to modify the image variants using Affinity Designer (similar recipes exist for other vector-based graphics software) and create a new SVG symbol file. The following diagram shows the modifications made to the calendar Regular-M variant to create the calendar image. Now, we need to modify all the remaining 26 image variants. Although each of the weight variants for the symbol are individually designed, Apple notes that the scale variants can be computed as a scale factor: Small is 0.783 times Regular, Large is 1.29 times Regular.
Verify new symbol file imports into Xcode’s Asset Catalog
The final step is to drag the newly created SVG file into Xcode’s asset catalog. If it imports cleanly, all 27 image variants are visible in the asset catalog as shown below for the calculator symbol example.
SwiftUI code for standard SF Symbols and custom symbols:
The code for inserting a standard SF Symbol using SwiftUI and adding modifiers is clean and swifty. A little more code is needed to process a custom symbol similarly in SwiftUI. Both implementations are shown below:
A screenshot of this code showing both symbols (calendar and calculator) is shown below:
With a little perseverance, the steps for creating a custom symbol are easily executed. The benefit is that the custom symbol supports all the dynamic typing (scale, weight, color) almost as simple as using standard SF Symbols.
Creating Custom Symbol Images for your App by Apple.
Configuring and Displaying Symbol Images in your UI by Apple.
Human Interface Guidelines: SF Symbols by Apple.
SF Symbols: The benefits and how to use them guide written by Antoine van der Lee.
SF Symbols written by Ivy Knight.
WWDC 2019 Session 206: Introducing SF Symbols presented by Paolo Mazzetti and Tom Adriaenssen.