我有两个文本视图。它们水平放置在一起。我怎样才能对齐它们,使得视图的交汇点也是容器视图的中心,不管任何一个字符串有多长?
例如...
这是第一个字符串|第二个字符串。
此处的管道将是容器视图的中心。显然,除非两个字符串的宽度完全相同,否则简单的 HStack 是行不通的。(旁注:在我的特定用例中,字符串不会太长以至于需要截断或换行,但这可能对其他有此问题的人有用)。
您可以.frame(maxWidth: .infinity)
在两个Text
s上使用 a ,这最终会使它们的宽度相等(父级的 50%)。
struct ContentView : View {
var body: some View {
VStack {
HStack(spacing: 2) {
Text("Short")
.frame(maxWidth: .infinity, alignment: .trailing)
.border(Color.blue)
Text("Longer. This is longer text....")
.lineLimit(1) // remove/change this if you want it to accommodate multiple lines
.frame(maxWidth: .infinity, alignment: .leading)
.border(Color.green)
}
}
}
}
您可以alignment
根据需要在每个上玩。当然,边框仅用于调试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句