这是我更喜欢的可能方法(至少在开始时),因为两个标签保持独立,并且始终通过主要文本在父容器中居中。
struct DemoCenteredText: View {
var body: some View {
GeometryReader { gp in
ZStack {
Text("Primary Text").font(.title)
.alignmentGuide(VerticalAlignment.center, computeValue: { $0[.bottom] })
.position(x: gp.size.width / 2, y: gp.size.height / 2)
Text("Secondary Text")
.alignmentGuide(VerticalAlignment.center, computeValue: { $0[.top] - 16 })
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
.position上面的in 将主标题固定在父容器的中心,可用空间由 消耗GeometryReader,并alignmentGuide强制布局将辅助文本放置在距主文本底部偏移 16 处(实际上与您问题中的约束方式相同)。
| 归档时间: |
|
| 查看次数: |
3230 次 |
| 最近记录: |