Wak*_*Wak 5 text alignment swift swiftui
我有以下代码:
struct ContentView: View {
var body: some View {
Text("Test")
.foregroundColor(.white)
.font(.subheadline)
.frame(maxWidth: .infinity)
.alignmentGuide(.leading) { d in d[.leading] }
.background(Color(.blue))
}
}
Run Code Online (Sandbox Code Playgroud)
但是正如您在下面的图像中看到的那样,它不会左对齐文本。有谁知道为什么会这样?也许是因为我使用的是 maxWidth,alignmentGuide 认为它已经左对齐了?
Asp*_*eri 14
因为alignmentGuide 在容器中与其他子视图一起起作用。在这种情况下,您需要Text在自己的框架内对齐。
这是解决方案
Text("Test")
.foregroundColor(.white)
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading) // << here !!
.background(Color(.blue))
Run Code Online (Sandbox Code Playgroud)