【发布时间】:2022-11-03 01:54:54
【问题描述】:
我想为我的图表设计类似的东西。使用我的代码,我实现了这样的目标。
文本未与垂直线居中对齐。愿我实现这一目标的方法完全错误。任何帮助表示赞赏。
我使用了以下代码。
struct FirstFormatterIndicator: View {
var text: String
var body: some View {
VStack(alignment: .leading, spacing: 0) {
Rectangle().frame(width: 1, height: 5)
.foregroundColor(Color.black)
Text(text)
.font(.system(size: 9))
}
}
}
struct EndFormatterLabel: View {
var text: String
var body: some View {
VStack(alignment: .trailing, spacing: 0) {
HStack(alignment: .top, spacing: 0) {
Rectangle().frame(height: 1)
.foregroundColor(Color.gray)
Rectangle().frame(width: 1, height: 5)
.foregroundColor(Color.gray)
}
Text(text)
.font(.system(size: 9))
}
}
}
struct WeeklyFormatterView: View {
var texts = ["S", "M", "T", "W", "T", "F", "S"]
var body: some View {
ZStack(alignment: .leading) {
FirstFormatterIndicator(text: texts.first!)
HStack(alignment: .top, spacing: 0) {
ForEach(1..<texts.count, id: \.self) { index in
EndFormatterLabel(text: texts[index])
.frame(maxWidth: .infinity)
}
}
}
.padding(.horizontal)
}
}
【问题讨论】: