简体   繁体   English

如何使用动态文本将 alignment 保留在列表中 - SwiftUI

[英]How to keep alignment in list with dynamic text - SwiftUI

I've got a list of items that display a title, a subheading and their distance.我有一个显示标题、副标题及其距离的项目列表。 The subheading and title can both change in size and may be smaller or large depending on data.副标题和标题的大小都可以改变,并且可以根据数据变小或变大。 The problem is I want the Text(distance) to remain in the same position regardless of the heading or subheading size.问题是无论标题或副标题大小如何,我都希望文本(距离)保持在相同的 position 中。


    var body: some View {
        VStack {
            Divider()
            HStack {
                Circle()
                    .fill(index < 7 ? .blue : .white)
                    .frame(width: 10, height: 10)
                    .padding(.leading, 4)

                Circle()
                    .fill(getColor(index: index))
                    .scaledToFit()
                    .frame(height: 35)
                    .clipShape(Circle())

                HStack {
                    VStack (alignment: .leading) {
                        Text(title)
                            .fontWeight(.medium)
                            .minimumScaleFactor(20)
                            .foregroundColor(.black)

                        Text(subheading)
                            .font(.subheadline)
                            .lineLimit(2)
                            .foregroundColor(.secondary)
                            .multilineTextAlignment(.leading)
                    }
                }
                Text("\(distance)")
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                
                Spacer()
                Spacer()
            }.frame(maxWidth: .infinity)
        }
    }

I've got some circles in there too that are based on the design, but they can be ignored.我也有一些基于设计的圆圈,但可以忽略它们。 My issue is towards the bottom of the HStack.我的问题是针对 HStack 的底部。 Trying to add padding etc. hasn't worked and causes strange renders of the view.尝试添加填充等无效,并导致视图呈现奇怪的效果。

I would have added this as a comment, however, I am not sure what you mean by keeping the Text("\(distance)") the same position.我会添加这个作为评论,但是,我不确定你所说的保持Text("\(distance)")相同 position 是什么意思。

You may be able to use a ZSTack and then set the .offset of the Text field, but this would mean that the distance text may overlap the other text您可以使用ZSTack ,然后设置文本字段的.offset ,但这意味着距离文本可能会与其他文本重叠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM