How to style Text in SwiftUI?

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

In this blog post, we’re going to explore how to style text in SwiftUI. Text is a fundamental element in any app and being able to control its appearance is crucial for creating a polished, professional-looking user interface. In this post, we’ll cover some of the ways you can adjust the font, color, and other attributes of text in SwiftUI.

One of the most basic ways to style text in SwiftUI is by adjusting the font. The font() modifier can be used to set the font of a text view. You can specify the font to be the system font at a certain size, for example:

Text("Hello, World!")
    .font(.system(size: 24))

You can also use custom fonts by providing the font’s name. For example, the following code sets the text’s font to “Avenir Next” with a size of 24 points:

Text("Hello, World!")
    .font(.custom("Avenir Next", size: 24))

In addition to adjusting the font, you can also make text bold or italic, or adjust the font weight or design using the .bold(),.italic() etc.

Text("Hello, World!")
    .font(.system(size: 24, weight: .bold, design: .rounded))

The foregroundColor() modifier can be used to change the text’s color. For example, the following code sets the text’s color to red:

Text("Hello, World!")

You can also use the background() modifier to change the background color of the text view. For example, the following code sets the text’s background color to green:

Text("Hello, World!")

Another interesting feature is the ability to adjust the spacing between characters, lines and paragraphs tracking() adjust character spacing, lineSpacing() adjust line spacing.

Text("Hello, World!")

Finally, SwiftUI also provides a variety of other text-related modifiers, such as lineLimit(), multilineTextAlignment(), and truncationMode(), that can be used to control the appearance of text in various ways.

In conclusion, SwiftUI provides a powerful set of tools for styling text. By using the various font, color, spacing and other modifiers, you can create text that looks exactly the way you want it to, and that fits perfectly into your app’s design.

✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn

Get Ready to Shine: Mastering the iOS Interview

Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.