我有一个 TextField,它应该同时包含短文本和长文本,旁边有一个标签,但标签始终挂在中间并且不与顶部对齐。尝试了所有可能的解决方案,但没有一个有效。
场景一:短文本
|-------|-------------------------------------------|
| Text | Lorem Ipsum is simply dummy text |
|---------------------------------------------------|
场景2:长文本
|-------|-------------------------------------------|
| Text | Lorem Ipsum is simply dummy text of the |
| | printing and typesetting industry. Lorem |
| | Ipsum has been the industry's standard |
| | dummy text ever since the 1500s |
|---------------------------------------------------|
这就是我总是在下面得到的长文本,标签总是在中心而不是顶部。尝试了网上找到的所有方法均无效。
场景2:长文本
|-------|-------------------------------------------|
| | Lorem Ipsum is simply dummy text of the |
| | printing and typesetting industry. Lorem |
| Text | Ipsum has been the industry's standard |
| | dummy text ever since the 1500s |
|---------------------------------------------------|
尝试过的代码:
HStack {
VStack {
Text("Label: ")
.lineLimit(1)
.padding(.bottom, 20)
}
.background(.teal)
VStack(alignment: .leading) {
Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s ")
.multilineTextAlignment(.leading)
}
.background(.tertiary)
}
.background(.blue)
您可以简化布局,例如:
将外部 HStack 对齐方式更改为
.top
. 这是.center
默认的。