使用Swiftui中的自定义对齐指南在2个不同的堆栈中对齐视图

发布于 2025-02-03 13:54:50 字数 3474 浏览 3 评论 0 原文

我有2个hstack坐在一个Vstack中,如下所示:

”在此处输入图像描述”

我要实现的目标是将标题2和标题4的领先者彼此保持一致。也没有使用框架彼此之间的标题1和标题3。如下:

“在此处输入图像描述”

这是我使用Horizo​​nTalAlignment的代码(为方便起见,我还将预览包含在预览中):

import SwiftUI

public struct ContentView: View {
    public var body: some View {
        VStack(alignment: .centerHorizontalAlignment, spacing: 10) {
            HStack(alignment: .top, spacing: 16) {
                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 1")
                        .font(.body)
                        .fontWeight(.medium)

                    Text("123456789")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))

                }
                .background(Color.green)

                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 2")
                        .font(.body)
                        .fontWeight(.medium)

                    Text("2 lines\nof text")
                        .font(.body)
                        .foregroundColor(.gray)

                }
                .background(Color.red)
                .alignmentGuide(.centerHorizontalAlignment) { $0[HorizontalAlignment.leading] }

            }
            .padding()
            .background(Color.purple)


            HStack(alignment: .top, spacing: 16) {
                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 3")
                        .font(.body)
                        .fontWeight(.medium)


                    Text("Aaaaaaaaaa")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))

                    Text("Bbbbbbbbbbbbbbbbbbbb")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))
                }
                .background(Color.yellow)

                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 4")
                        .font(.body)
                        .fontWeight(.medium)


                    Text("Cccccccccccc")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))
                }
                .background(Color.blue)
                .alignmentGuide(.centerHorizontalAlignment) { $0[HorizontalAlignment.leading] }

            }
            .font(.body)
            .padding()
            .background(Color.gray)


        }
        .padding()
        .frame(alignment: .leading)
        .background(Color.cyan)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension HorizontalAlignment {
    enum CenterHorizontalAlignment: AlignmentID {
        static func defaultValue(in d: ViewDimensions) -> CGFloat {
            d[HorizontalAlignment.center]
        }
    }

    static let centerHorizontalAlignment = HorizontalAlignment(CenterHorizontalAlignment.self)

}

如果有人可以给我一些有关如何实现此目的的提示或解决方案,则将不胜感激。 我正在使用Xcode 13.3.1和Swift5。PS

文本是完全通用和动态的。因此,不能保证这里总是有1或2行文本。我希望该解决方案足够通用,因此无论内容多长时间,它都可以正常工作。

I have 2 HStacks sitting in one VStack as follows:

enter image description here

What I'm trying to achieve is to align the leading of Title 2 and Title 4 with each other. And also Title 1 and Title 3 to each other without using frames. Like the following:

enter image description here

Here is my code using HorizontalAlignment (I have also included the preview for convenience):

import SwiftUI

public struct ContentView: View {
    public var body: some View {
        VStack(alignment: .centerHorizontalAlignment, spacing: 10) {
            HStack(alignment: .top, spacing: 16) {
                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 1")
                        .font(.body)
                        .fontWeight(.medium)

                    Text("123456789")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))

                }
                .background(Color.green)

                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 2")
                        .font(.body)
                        .fontWeight(.medium)

                    Text("2 lines\nof text")
                        .font(.body)
                        .foregroundColor(.gray)

                }
                .background(Color.red)
                .alignmentGuide(.centerHorizontalAlignment) { $0[HorizontalAlignment.leading] }

            }
            .padding()
            .background(Color.purple)


            HStack(alignment: .top, spacing: 16) {
                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 3")
                        .font(.body)
                        .fontWeight(.medium)


                    Text("Aaaaaaaaaa")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))

                    Text("Bbbbbbbbbbbbbbbbbbbb")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))
                }
                .background(Color.yellow)

                VStack(alignment: .leading, spacing: 10) {
                    Text("Title 4")
                        .font(.body)
                        .fontWeight(.medium)


                    Text("Cccccccccccc")
                        .font(.body)
                        .foregroundColor(.black.opacity(0.6))
                }
                .background(Color.blue)
                .alignmentGuide(.centerHorizontalAlignment) { $0[HorizontalAlignment.leading] }

            }
            .font(.body)
            .padding()
            .background(Color.gray)


        }
        .padding()
        .frame(alignment: .leading)
        .background(Color.cyan)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension HorizontalAlignment {
    enum CenterHorizontalAlignment: AlignmentID {
        static func defaultValue(in d: ViewDimensions) -> CGFloat {
            d[HorizontalAlignment.center]
        }
    }

    static let centerHorizontalAlignment = HorizontalAlignment(CenterHorizontalAlignment.self)

}

Would be appreciated if someone can give me some hints or solutions on how to achieve this.
I'm using Xcode 13.3.1 and Swift 5.

P.S. The text is fully generic and dynamic. So there is no guarantee that there are always 1 or 2 lines of text here. I would like this solution to be generic enough so it works no matter how long the content is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2025-02-10 13:54:50

我认为您想要自定义对齐指南:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文