SwiftUI 文本与 maxWidth 对齐不起作用
SwiftUI Text alignment with maxWidth not working
我有以下代码:
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))
}
}
但是正如您在下面的图片中看到的那样,它没有左对齐文本。有谁知道为什么会这样?
也许是因为我使用的是 maxWidth,alignmentGuide 认为它已经左对齐?
因为 alignmentGuide 在容器中与其他子视图一起生效。在这种情况下,您需要在自己的框架内对齐 Text
。
这是解决方案
Text("Test")
.foregroundColor(.white)
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading) // << here !!
.background(Color(.blue))
我有以下代码:
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))
}
}
但是正如您在下面的图片中看到的那样,它没有左对齐文本。有谁知道为什么会这样? 也许是因为我使用的是 maxWidth,alignmentGuide 认为它已经左对齐?
因为 alignmentGuide 在容器中与其他子视图一起生效。在这种情况下,您需要在自己的框架内对齐 Text
。
这是解决方案
Text("Test")
.foregroundColor(.white)
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading) // << here !!
.background(Color(.blue))