xcode 删除SwiftUI中的文本水平填充

ztyzrc3y  于 2023-02-05  发布在  Swift
关注(0)|答案(1)|浏览(124)

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()
    }
}

nafvub8i

nafvub8i1#

这不是填充。默认情况下,文本框会居中对齐,因此文本无法与屏幕完全对齐。因此,您可能需要执行以下操作:

VStack (alignment: .leading) {
...
}

你也可以像在第一行中一样,在HStack中添加Spacer,这将把文本推到边缘。

相关问题