更改文本宽度而不增加堆栈宽度

时间:2020-04-13 22:31:35

标签: swift swiftui

enter image description here

  let months = ["january" , "februarry", "march", "april", "may", "june", "july", "august",    "september", "october", "november", "december"]
   var body: some View {
        HStack {
            ForEach(months, id: \.self) { month in
                VStack {
                    if month == "october" {
                        Text(month)
                    }
                    Rectangle()
                        .frame(width: 25, height: 10)
                }
            }
        }
    }

如何在不改变VStack宽度以及方格之间的距离的情况下显示文本的全长。

它应该像这样:

It should look like this

3 个答案:

答案 0 :(得分:0)

不清楚您在这里要问什么,但您是否尝试过以下方法:

let months = ["january" , "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]

var body: some View {
    ScrollView (.horizontal, showsIndicators: false) {
        HStack {
            ForEach(months, id: \.self) { month in
                VStack {
                    Text(month).fixedSize()  // <--- this
                    Rectangle().frame(width: 25, height: 10)
                }
            }
        }
    }
}

答案 1 :(得分:0)

好吧,那该怎么办?

let months = ["january" , "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
@State var chosenMonth = ""

var body: some View {
    VStack {
        HStack {
            Spacer()
            Text(chosenMonth)
            Spacer()
        }
        HStack {
            ForEach(self.months, id: \.self) { month in
                Rectangle().frame(width: 25, height: 10).onAppear(perform: {
                    if month == "october" {
                        self.chosenMonth = month
                    }
                })
            }
        }
    }
}

答案 2 :(得分:0)

这里是基于对齐指南的可能方法。使用Xcode 11.4 / iOS 13.4进行了测试

想法是在不同的图层上使用条形和文本,并使用自定义对齐指南对齐它们,并处理拐角处的情况以防止文本掉出屏幕。

demo

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

    static let monthAlignment = HorizontalAlignment(MonthAlignment.self)
}

struct DemoMonthAlignment: View {
    let months = ["january" , "februarry", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
    @State private var selectedMonth = "october"
    var body: some View {


        VStack(alignment: .monthAlignment, spacing: 2) {
            Text(selectedMonth)
                .alignmentGuide(.monthAlignment, computeValue: { d in
                    if self.selectedMonth == self.months.first {
                        return d[.leading]
                    } else if self.selectedMonth == self.months.last {
                        return d[.trailing]
                    } else {
                        return d[HorizontalAlignment.center]
                    }
                })
            HStack {
                ForEach(months, id: \.self) { month in
                    Group {
                        if month == self.selectedMonth {
                            Rectangle()
                                .foregroundColor(Color.red)
                                .alignmentGuide(.monthAlignment, computeValue: { d in
                                    if month == self.months.first {
                                        return d[.leading]
                                    } else if month == self.months.last {
                                        return d[.trailing]
                                    } else {
                                        return d[HorizontalAlignment.center]
                                    }
                                })
                        } else {
                            Rectangle()
                        }
                    }
                    .frame(width: 25, height: 10)
                }
            }
        }
    }
}