Wesley de Groot's Blog
Interpolation and formatting in Text

Back

In this post, we will explore how to use interpolation and formatting in SwiftUI's Text view. This is a powerful feature that allows you to create dynamic and formatted text easily.

What is Interpolation and Formatting?

Interpolation and formatting in SwiftUI's Text view refer to the ability to insert dynamic values into text strings and apply various formatting options. This allows developers to create rich, user-friendly text displays that can change based on user input or other data.

Interpolation in SwiftUI

Interpolation in SwiftUI allows you to embed variables and expressions directly within a string. You can use string interpolation by wrapping your variables in parentheses and prefixing them with a backslash. Here's an example:

import SwiftUI

struct ContentView: View {
    let name = "Wesley"
    let age = 35

    var body: some View {
        Text("Hello, my name is \(name) and I am \(age) years old.")
    }
}

In this example, the Text view displays a greeting message that includes the name and age variables.

Formatting in SwiftUI

Formatting in SwiftUI allows you to control how values are displayed, such as arrays.

import SwiftUI

struct ContentView: View {
    var body: some View {
        let pokemon = ["Dragonite", "Lugia", "Pikachu"]

        Text("Favorite Pokemon: \(pokemon, format: .list(type: .and))")
        // Favorite Pokemon: Dragonite, Lugia and Pikachu
    }
}

In this example, the Text view formats the pokemon array into a list format, automatically handling the conjunction for the last item.

import SwiftUI

struct ContentView: View {
    var body: some View {
        let pokemon = ["Dragonite", "Lugia", "Pikachu"]

        Text("Favorite Pokemon: \(pokemon, format: .list(type: .and))")
            .environment(\.locale, Locale(identifier: "nl"))
        // Favorite Pokemon: Dragonite, Lugia en Pikachu
    }
}

Formatting Measurements

You can also format measurements in SwiftUI. For example, if you have a measurement in meters and want to display it in kilometers, you can do the following:

import SwiftUI

struct ContentView: View {
    var body: some View {
        let distance = Measurement(value: 1000, unit: UnitLength.meters)

        // You walked 3,281 mi.
        Text("You walked \(distance, format: .measurement(width: .abbreviated)).")
            .environment(\.locale, Locale(identifier: "en_US"))

        // You walked 1 km.
        Text("You walked \(distance, format: .measurement(width: .abbreviated)).")
            .environment(\.locale, Locale(identifier: "nl"))
    }
}

Caveats

While interpolation and formatting in SwiftUI's Text view are powerful, there are some caveats to keep in mind:

  • Performance: Excessive use of interpolation and formatting can lead to performance issues, especially in complex views. Use them judiciously.

  • Localization: When using interpolation and formatting, ensure that your strings are localized properly to support different languages and regions. SwiftUI provides tools for localization, but you need to manage your strings carefully.

Wrap up

In this post, we explored how to use interpolation and formatting in SwiftUI's Text view. By leveraging these features, you can create dynamic and user-friendly text displays that adapt to your app's needs.

Resources:

Read more

Share


Share Bluesky Mastodon Twitter LinkedIn Facebook
x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap