|

How to Make Text Bold in iOS SwiftUI

Bold text is far more than a mere stylistic choice; it serves as a powerful tool that can greatly enhance the overall user experience of your app. It adds emphasis, creates visual hierarchies, and guides the user’s eye to key information.

Using bold text effectively is important in delivering a clear, intuitive, and user-friendly mobile app interface. In this blog post, let’s learn how to make text bold in SwiftUI.

Basics: Make Text Bold

To make text bold in SwiftUI, you can use the bold() modifier. This modifier applies to a Text view and turns the font weight to bold. Here’s an example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, SwiftUI!")
            .bold()
    }
}

In this code snippet, we have a text view displaying “Hello, SwiftUI!” in bold.

SwiftUI text bold

Combine Bold with Other Modifiers

The power of SwiftUI lies in the composability of its views. You can combine the bold() modifier with other modifiers to achieve various text styles. For instance, you can combine bold() with font():

Text("Hello, SwiftUI!")
    .font(.title)
    .bold()

In this example, the text “Hello, SwiftUI!” will appear in bold and also in the font size corresponding to .title.

swifui bold text

Customize Boldness

While SwiftUI does not provide an out-of-the-box solution for varying the weight of bold text, you can achieve this by using the .fontWeight() modifier with custom Font.Weight values:

Text("Hello, SwiftUI!")
    .fontWeight(.heavy)
swiftui text fontweight

In the above example, we’re using the .heavy font weight, which is even bolder than the standard bold weight. Other predefined font weights include .black, .bold, .medium, .light, .semibold, .thin, and .ultraLight.

Mastering text manipulation in SwiftUI, including creating bold text, is key to developing a visually effective and user-friendly app UI. By combining different modifiers and taking advantage of SwiftUI’s flexibility, you can greatly enhance the way your text appears to end users.

Similar Posts

Leave a Reply