Amirca
Amirca

Reputation: 313

Remove Text Horizontal Padding in SwiftUI

There is extra horizontal padding for text in SwiftUI. This extra padding appears in some devices (canvas, simulator, and physical device) like iPhone 14 Pro, while it does not exist in other devices like iPhone 14 Pro Max (xCode Version 14.2). I'd like to have the text and H1 aligned leading. How can I remove the extra padding?

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

enter image description here

Upvotes: 0

Views: 1777

Answers (1)

Ivan I
Ivan I

Reputation: 9990

This is not a padding. The box of your text is aligned to the center by default and text can't be of exact with of the screen. So you probably want to do something like:

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

Also you could do the same trick that you did in the first line by adding Spacer in HStack which will push the text to the edge.

Upvotes: 6

Related Questions