How to create a Styleguide in SwiftUI

A step by step tutorial describing our method for creating a source of truth for custom styled text in SwiftUI. Github repository included.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 08/19/2024 at 11:38
Last Updated on 10/16/2024 at 14:38
<p>Two flowers that represents SwiftUI and XCode side by side with the text “SwiftUI Styleguide” beneath it.</p>

A step by step tutorial describing our method for creating a source of truth for custom styled text in SwiftUI. Github repository included.

SubscribeDownload Open Source SwiftUI Starter ProjectDownload Open Source VisionOS Starter Project

The following tutorial is based on our styleguide tutorial for Swift and has been adapted to work with SwiftUI and Swift previews.

How to create a Styleguide in XCode and SwiftStyleguide: What it is & How to create one

The example can be found on the main branch of our SwiftUI Open Source Starter Project linked below.

Download Open Source SwiftUI Starter Project

Please note that at this time it is not possible to overwrite the standard Font styles (i.e. .title, .body) in SwiftUI.

The key differences from our Swift example are the following:

Fonts

<p>A screenshot of XCode showing how we have declared the fonts.</p>

The following updates have been made to the Fonts.

  • We have placed the Fonts under the Styleguide folder (previously under Models/Constants).
  • We have made all the font definitions static to facilitate the use of these constants.

Colors

<p>A screenshot of XCode showing the Assets folder on the left hand side and the color definitions on the right hand side.</p>

The following updates have been made to the Fonts and colors.

  • Colors have been declared as Color Sets within the Assets of the app. This is beneficial as they will automatically adjust to the relevant color scheme, removing a lot of code from our Styleguide.
  • Similarly to the fonts, we have defined the Colors using static constants within a Color structure found within the Styleguide folder (previously under Models/Constants).
  • Colors now use Color type instead of UIColor and map to a text definition that matches a color set within the Assets of the app.

Please note that you must NOT name color sets "Red", "Green", "Primary" or "Background" as these conflict with the Apple System definitions. Apple has yet to provide a means for developers to alter the standard color scheme.

How to create and use a Color Set in XCode

Color Scheme

<p>A screenshot of XCode showing the styleguide file.</p>

As we are using Color Sets, we no longer need to adjust based on color scheme - this is done for us automatically.

Consult the "Colors" section above and associated tutorial if you are not following how this works.

Debug Fonts

<p>A screenshot of the Styleguide highlighting how you can debug fonts.</p>

We have updated the code so that you can print available fonts by changing a boolean in the Styleguide.

All in One File

<p>A screenshot of the Styleguide highlighting how all the styles are now in one file.</p>

Although we recommend separate files for complex styleguide, we have chosen to place all the colors and styles in the Styleguide declaration file.

Looking to learn more about SwiftUI, Swift, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe