SwiftUI Text init with vebratim parameter


Greetings, traveler!

When diving into SwiftUI development, one of the fundamental components you’ll frequently interact with is Text. SwiftUI’s Text initializer offers several ways to initialize text content. Today we will explore two of them: with the verbatim parameter and without it.

Without verbatim

The standard initializer for Text in SwiftUI looks like this:

Text("Hello, World!")

When you initialize Text without the verbatim parameter SwiftUI automatically applies localization. This means if your app supports multiple languages, the text can be translated based on the user’s language preference.

With vebratim

Here’s how you use the verbatim initializer:

Text(verbatim: "Hello, World!")

Using the verbatim parameter is good way to treat the text exactly as written, with no localization applied.

Example

struct GreetingsView: View {
    var body: some View {
        VStack {
            Text("Greetings, traveler!") // "Saludos, viajero!" in Spanish
            Text(verbatim: "Greetings, traveler!") // No localization
        }
    }
}