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...

2 answers

  • answered 2022-05-04 12:19 azamsharp

    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()
        }
    }
    

  • answered 2022-05-04 17:44 Mr Developer

    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.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum