3

I am trying to have 3 text views and an image view inside a vstack and have them align differently.

Here is my code:

VStack(alignment: .leading) {
        Text("This is the first paragraph").foregroundColor(Color.red)
        Spacer().frame(height: 10)
        Text("This is a paragraph with ").foregroundColor(Color.blue) + Text("multiple colors.").foregroundColor(Color.green)
        Spacer().frame(height: 10)
        if #available(iOS 15.0, *) {
            AsyncImage(url: URL(string: "https://www.example.org/img/image.png")) { image in
                image.resizable()
            } placeholder: {
                Color.red
            }
            .frame(width: 128, height: 128)
            .clipShape(RoundedRectangle(cornerRadius: 25))
        }
        Spacer().frame(height: 10)
        Text("Final paragraph").foregroundColor(Color.red)
        Spacer()
    }

Here in the below image I would like the first line and the image to be center aligned: enter image description here

I tried it like below but then the left aligned text didn't leave a margin like the above image.

struct ContentView: View {
var body: some View {
    VStack {
        HStack {
            Text("This is the first paragraph").foregroundColor(Color.red).frame(alignment: .leading)
        }
        Spacer().frame(height: 10)
        HStack {
            Text("This is a paragraph with ").foregroundColor(Color.blue) + Text("multiple colors.").foregroundColor(Color.green)
            Spacer()
        }
        Spacer().frame(height: 10)
        HStack {
            if #available(iOS 15.0, *) {
                AsyncImage(url: URL(string: "https://www.example.org/img/image.png")) { image in
                    image.resizable()
                } placeholder: {
                    Color.red
                }
                .frame(width: 128, height: 128)
                .clipShape(RoundedRectangle(cornerRadius: 25))
            }
        }
        Spacer().frame(height: 10)
        HStack {
            Text("Final paragraph").foregroundColor(Color.red)
            Spacer()
        }
        Spacer()
    }
  }
}

The view: enter image description here

1
  • It is not a "margin" - you just see centered VStack wrapped most wide child view, so if your 2nd text will be wider there will be no "margin" anymore. So which layout you need at the end, eg. for any long text at 2nd line? Commented Feb 8, 2022 at 18:11

1 Answer 1

3

I can assume that you just need padding in right place to have something like "margins", so a solution can be like

demo

var body: some View {
    VStack(alignment: .leading) {
        Text("This is the first paragraph")
            .foregroundColor(Color.red)
            .frame(maxWidth: .infinity)    // << default center !!

        Spacer().frame(height: 10)
        Text("This is a paragraph with ").foregroundColor(Color.blue) + Text("multiple colors.").foregroundColor(Color.green)
        Spacer().frame(height: 10)
        if #available(iOS 15.0, *) {
            AsyncImage(url: URL(string: "https://www.example.org/img/image.png")) { image in
                image.resizable()
            } placeholder: {
                Color.red
            }
            .frame(width: 128, height: 128)
            .clipShape(RoundedRectangle(cornerRadius: 25))
            .frame(maxWidth: .infinity)  // << for center !!
        }
        Spacer().frame(height: 10)
        Text("Final paragraph").foregroundColor(Color.red)
        Spacer()
    }
    .padding(.horizontal, 24)   // << for margins !!
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Is there anyway to specify the padding to use something like safeArea helpers so that the value doesn't have to be hardcoded?
By using just .padding(.horizontal) there will be used default margins. Safe area is taking into account automatically unless you turn it off explicitly with .ignoresSafeArea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.