aheze
aheze

Reputation: 30534

SwiftUI - how to align multiline Text to left side?

I'm trying to get Text to touch the left side. Right now, they all have unequal spacing on the sides. Here's my code:

struct ContentView: View {
    var body: some View {
        VStack(spacing: 20) {
            Text("Some long text long longgggggg long long long long long long long long long long long long")
                .border(Color.blue)
            
            Text("Some long text long a long long long long long long long long long long long long")
                .border(Color.blue)
            
            Text("Some long text long asda long long long long long long long long long long long long")
                .border(Color.blue)
        }
    }
}

Result:

3 Text stacked vertically, each with different widths and centered

This is what I want (achieved with HStack + Spacer()):

3 Text stacked vertically, each with different widths but aligned left

How can I do this? HStack + Spacer() doesn't seem right for multiline Text.

Upvotes: 1

Views: 990

Answers (1)

aheze
aheze

Reputation: 30534

I figured it out as I was writing my question. Just add alignment: .leading to the VStack.

struct ContentView: View {
    var body: some View {
        VStack(alignment: .leading, spacing: 20) { /// here!
            Text("Some long text long longgggggg long long long long long long long long long long long long")
                .border(Color.blue)

            Text("Some long text long a long long long long long long long long long long long long")
                .border(Color.blue)

            Text("Some long text long asda long long long long long long long long long long long long")
                .border(Color.blue)
        }
    }
}

Result:

3 Text stacked vertically, each with different widths but aligned left

Upvotes: 3

Related Questions