In this article we are going to see the font and text modifiers in SwiftUI and how to use them for our text views in our iOS apps. Typography and fonts play an important and fundamental role in our User Interface design, influencing the user experience of our app.
It is important that in our iOS app the user interface is intuitive.
Typography and fonts are crucial elements of UI/UX design that have a huge impact on user experience. They help set the mood, convey information, and make using an app or website more enjoyable. Let’s take the detailed view of a recipe app as an example to understand why typography and fonts are important in our iOS app.
The app pays attention to typography. For titles, use a clean, modern font so they stand out and are organized. For the main text, choose a readable and attractive font for comfortable reading.
The app also uses different font weights to show the importance of the information. Recipe titles are bolded to attract attention, while the ingredient list and instructions use normal weight for easy reading.
By paying attention to the texts the app creates a better user experience.
Keep up with consistency: Consistency is key when it comes to text styles in your iOS app. Choose an essential text style or a text style that matches your app’s markup and stick to it throughout your app. Using predictable text styles creates a strong, clean customer experience.
Think about understandability: Focus on clarity when choosing text styles for your application. Make sure the selected text styles are clear and easy to read, even in different text sizes or on different devices. Avoid overly bright or disconcerting text styles that can block meaning.
Design a visual ordered progression: Use styles, loadings, and text style sizes to design a visual ordered progression at the endpoint of your application. Separate headings, titles, and important information from standard text using bolder fonts or larger sizes. Unmistakable visual order guides customers and improves the overall customer experience.
Opening Test: Make sure the textual styles you choose are available to clients with fluctuating visual abilities. Test your app’s text styles with various aperture settings, including dynamic sorting, to ensure that text remains decipherable and doesn’t lose its clarity or separation. Consider using text styles or quotes that can accommodate customers’ availability inclinations.
Embrace customization: While framework text styles provide consistency across iOS devices, be sure to integrate custom text styles when they align with your app’s design and markup goals. Custom text styles can add character and uniqueness to your application. Simply ensure that custom text styles remain consistent and coordinate consistently with the overall application plan.
I want to believe that I can push you to work with textual styles and texts. They are an important piece of daily improvement work. Now we should understand how they are used in SwiftUI.
How to style text font in SwiftUI
Text is a view that displays at least one line of text in SwiftUI.
Text("Hello, world!")
We can replace this default text style with a custom one. We can set a custom text style for a text view using the font(_:) modifier.
We should investigate various ways to modify a text in SwiftUI.
Text style is a way of communicating a text style with respect to its capability.
For example, if you need to set a text style for your view’s title text, you can set it to title text style without determining the dimension of the text. Apple does the difficult work of characterizing the ideal size for each text style.
Adopting text styling allows you to exploit dynamic sorting where your text consequently resizes, tracks and drives in view of the customer’s inclination.
Apple has 11 text styles right now: largeTitle, title, title2, title3, headline, body, callout, subheadline, footnote, caption, and caption2.
Text("Swift Programming")
.font(.largeTitle)
Text("Swift Programming")
.font(.title)
Text("Swift Programming")
.font(.title2)
Text("Swift Programming")
.font(.title3)
Text("Swift Programming")
.font(.headline)
Text("Swift Programming")
.font(.body)
Text("Swift Programming")
.font(.callout)
Text("Swift Programming")
.font(.subheadline)
Text("Swift Programming")
.font(.footnote)
Text("Swift Programming")
.font(.caption)
Text("Swift Programming")
.font(.caption2)
Compiled in XCode
How to change text size in SwiftUI
We have four font designs to choose from:
default
monospaced
rounded
serif
We can configure it at the time we create a font.
If you want to customize the font design of a text style, you can set it like this:
Text("Hello, world!")
.font(.system(.largeTitle, design: .serif))
Compiled in XCode
How to change font weight in SwiftUI
We can use the modifier weight(_:). This is a font instance method, because we use this in our font, not in a text.
We can use heavy, light or bold.
For example in:
Text("Swift programming")
.font(.largeTitle)
Text("Swift programming")
.font(
.largeTitle
.weight(.bold)
)
Text("Swift programming")
.font(
.system(
.largeTitle,
design: .rounded
)
.weight(.light)
)
Text("Swift programming")
.font(
.system(size: 34)
.weight(.heavy)
)
In XCode
You can also use .weight in a custom font, but not all weights may be available in that font. The closest weight will be used as an alternative if the specification cannot be met.
Set the font weight with the Font initializer
You can set it when you initialize a custom system font. For example:
Text("Swift programming")
.font(
.system(size: 31, weight: .bold)
)
Change the font weight with the fontWeight(_:) modifier
The text view also has a way to modify the font weight. You can set it with the fontWeight(_:) modifier.
Since it is a text instance method, we use it on a text, not a font.
Text("Swift programming")
.font(
.system(size: 31)
)
.fontWeight(.bold)
Change font weight with bold() modifier
The font and text view also has another method for applying a bold font weight to text, bold().
Text("Swift programming")
.font(
.system(size: 31)
)
.bold()