当文本输入为多行时,我遇到了对齐问题。在本例中,第一部分显示了图像中正确显示的左侧和右侧文本。第二部分显示了3到4行文本,干扰了右侧对齐
我想让左边在多行中独立地调整大小。右边保持原样(2nd title
和2nd Value
之间没有间隙)。
我的视图代码:
struct ContentView: View {
var body: some View {
Form {
Section {
Text("single line")
}
TwoLineView(firstTitle: "1st Title", fistValue: "Value",
secondTitle: "2nd Title", secondValue: "2nd Value")
TwoLineView(firstTitle: "1st Title", fistValue: "test",
secondTitle: "2nd Title", secondValue: "2nd Value")
Section {
Text("mutiple lines")
}
TwoLineView(firstTitle: "1st Title", fistValue: "This is long. This is long value. This is long value",
secondTitle: "2nd Title", secondValue: "2nd Value")
TwoLineView(firstTitle: "1st Title", fistValue: "test",
secondTitle: "2nd Title", secondValue: "2nd Value")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
struct TwoLineView: View{
var firstTitle: String
var fistValue: String
var secondTitle: String
var secondValue: String
var body: some View {
HStack(alignment: VerticalAlignment.center, spacing: 0) {
VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
Text(firstTitle).lineLimit(1)
.font(.system(size: 30, weight: .heavy, design: .default))
Text(fistValue)
.frame(maxHeight: .infinity)
}
Spacer()
VStack(alignment: HorizontalAlignment.leading, spacing: 0) {
Text(secondTitle).lineLimit(1)
.font(.system(size: 30, weight: .heavy, design: .default))
Text(secondValue).lineLimit(1)
Spacer()
}
Spacer(minLength: 45)
}
}
}
预期:单行或多行值。我不希望在标题和值之间显示间隙。(图像中的第二部分在2nd title
和2nd Value
之间有间隙,这是糟糕的UX体验)
问题屏幕截图:-x1c 0d1x
3条答案
按热度按时间ldioqlga1#
只需删除
frame
,然后在VStack
中添加spacer
。zzoitvuj2#
lh80um4z3#
只需移除框架并在VStack中添加垫片。
这解决了问题,并显示我所需要的。但是,一个问题是它打破了右下角框中的对齐
[![在此输入图像说明][1]][1]