1

I need to implement view with two texts, and first text alignment need to be trailing, and second text alignment leading, but I have this.. please help me to build correct layout. i tried to put another VStack inside the HStack and assign the trailing orientation to the first and the leading to the second

enter image description here

this is my code example:

 var body: some View {
        VStack {
            HStack(spacing: 10) {
                Text("test1")
                    .font(.system(size: 12))
                    .fontWeight(.bold)
//                    .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 10))
                Text("testtest1")
                    .padding()
                    .font(.system(size: 14))
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.red, lineWidth: 1)
                    )
                Spacer()
            }
            .frame(width: UIScreen.screenWidth)
            .padding(EdgeInsets(top: 0, leading: 40, bottom: 12, trailing: 0))
            HStack(spacing: 10) {
                Text("test23")
                    .font(.system(size: 12))
                    .fontWeight(.bold)
//                    .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 10))
                Text(String("testtest12"))
                    .padding()
                    .font(.system(size: 14))
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.red, lineWidth: 1)
                    )
                Spacer()
            }
            .frame(width: UIScreen.screenWidth)
            .padding(EdgeInsets(top: 0, leading: 40, bottom: 12, trailing: 0))
            HStack(spacing: 10) {
                Text("test345")
                    .font(.system(size: 12))
                    .fontWeight(.bold)
                    .frame(alignment: .trailing)
//                    .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 10))
                Text("testtest1234")
                    .padding()
                    .font(.system(size: 14))
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.red, lineWidth: 1)
                    )
                Spacer()
            }
            .frame(width: UIScreen.screenWidth)
            .padding(EdgeInsets(top: 0, leading: 40, bottom: 12, trailing: 0))
            HStack {
                Text("test6789")
                    .font(.system(size: 12))
                    .fontWeight(.bold)
//                    .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 10))
                Text("testtest123456")
                    .padding()
                    .font(.system(size: 14))
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.red, lineWidth: 1)
                    )
                Spacer()
            }
            .frame(width: UIScreen.screenWidth)
            .padding(EdgeInsets(top: 0, leading: 40, bottom: 12, trailing: 0))
            HStack {
                Text("test01234")
                    .font(.system(size: 12))
                    .fontWeight(.bold)
//                    .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 10))
                Text("testtest56")
                    .padding()
                    .font(.system(size: 14))
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.red, lineWidth: 1)
                    )
                Spacer()
            }
            .frame(width: UIScreen.screenWidth)
            .padding(EdgeInsets(top: 0, leading: 40, bottom: 12, trailing: 0))
            HStack {
                Text("test5")
                    .font(.system(size: 12))
                    .fontWeight(.bold)
//                    .padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 10))
                Text("testtes78")
                    .padding()
                    .font(.system(size: 14))
                    .foregroundColor(.black)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 20)
                            .stroke(Color.red, lineWidth: 1)
                    )
                Spacer()
            }
            .frame(width: UIScreen.screenWidth)
            .padding(EdgeInsets(top: 0, leading: 40, bottom: 12, trailing: 0))
        }
    }

But I want to make something like this:

enter image description here

2
  • perhaps blog.logrocket.com/understanding-the-swiftui-grid-layout might be of interest Commented May 19, 2022 at 16:16
  • " i tried to put another VStack inside the HStack and assign the trailing orientation to the first and the leading to the second" Commented May 20, 2022 at 8:50

1 Answer 1

1

Give the texts on the left fixed width and add the multiLineAlignment(.traling) to all.

Edit:

var body: some View {
    VStack(spacing: 0) {
        ForEach(0..<5) { i in
            HStack(spacing: 0) {
                Text("Left Text \(String(repeating: "XX", count: i))")
                    .font(.system(size: 12).bold())
                    .frame(width: 150, alignment: .trailing)
                    .padding(.trailing, 15)
                Text("Right Text \(i)")
                    .font(.system(size: 12))
                    .padding()
                    .overlay(
                        RoundedRectangle(cornerRadius: 20, style: .continuous)
                            .stroke(.red)
                    )
                Spacer()
            }
            .padding(.bottom, 15)
            .frame(maxWidth: .infinity)
        }
        Spacer()
    }
    .padding()
    .frame(maxWidth: .infinity)
}

Preview

Sign up to request clarification or add additional context in comments.

3 Comments

Although this seems like a good solution, I think it would be nicer to add code to support the answer.
please, add the code
@user14300555 I added example code and preview.

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.