'Wrap truncated text in Double quotes in SwiftUI

Expected result (Truncated text wrapped in double-quotes): "Let's figure out your preferen..."

I tried using

Text("\"\(description)\"")
    .lineLimit(1)

but unable to get a double quote at the end "Let's figure out your preferen...



Solution 1:[1]

As others have said, you should give us some ideas of what you have tried. One possible solution can be the following.

struct ContentView: View {

let description: String = "Let's figure out your preferences and interests for this trip and get your trip sorted with the best recommendations that work for you"



   var body: some View {
        Text("\"\(description)\"")
            .padding()
    }
}

Solution 2:[2]

You have to individually escape each character. The following code would show a double quote at the end.

Text("\" This will show double quotes at the start and end \"")

This is done via escaping - you need to put a \ in front of each character that should be taken as a text character instead of code. Note how the code above is highlighted too - only the first and last quotes are part of the code, so only they are in a different color.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 azamsharp
Solution 2 Mr Developer