删除 SwiftUI 中的文本水平填充

Ami*_*rca 0 xcode padding swift swiftui

SwiftUI 中的文本有额外的水平填充。这种额外的填充出现在iPhone 14 Pro等某些设备(画布、模拟器和物理设备)中,而在iPhone 14 Pro Max (xCode 版本 14.2)等其他设备中则不存在。我想让文本H1对齐。如何去除多余的填充物?

struct TestView: View {
    var body: some View {
        VStack {
            HStack {
                Text("H1")
                    .font(.custom("GillSans-bold", size: 18))
                Spacer()
                Image(systemName: "doc")
            }
            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.")
                .font(.custom("GillSans", size: 18))
                .background(.cyan)
            
        }
        .background(.yellow)
        .padding()
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Iva*_*čin 5

这不是填充。默认情况下,文本框与中心对齐,并且文本不能与屏幕精确对齐。所以你可能想做类似的事情:

VStack (alignment: .leading) {
...
}
Run Code Online (Sandbox Code Playgroud)

Spacer您也可以通过添加HStack将文本推到边缘来执行与第一行中相同的技巧。