Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text() is adding extra leading trailing padding SwiftUI

Hi Im creating this design, with swiftUI but I can’t figure out why the Text() is adding more padding to the string value, I just want it to the leading trailing, like margins

Design:
enter image description here

Result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Code:

    struct ProductDetailSwiftUIView: View {
    
        var body: some View {
            ZStack {
                ScrollView {
                    VStack {
                        Rectangle()
                            .frame(height: 213)
                            .foregroundColor(Color.blue)
                            
                        Text("Fresas Congeladas La Huerta 500g")
                            .foregroundColor(.black)
                            .font(Font.bodySemibold(22))
                            .multilineTextAlignment(.leading)
                            .frame(maxWidth: .infinity)
                            .background(Color.red)
                            .padding(.leading, 16)
                            .padding(.trailing, 16)
                    }
                }.toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button {
                            print("Hello world🚀")
                        } label: {
                            Label("Back", image:  "cartLine")
                        }.overlay(Badge(count: 1))
                    }
                }
            }
        }
    }

>Solution :

At first Text is aligned centered in frame, so

Text("Fresas Congeladas La Huerta 500g")
    .foregroundColor(.black)
    .font(Font.bodySemibold(22))
    .multilineTextAlignment(.leading)
    .frame(maxWidth: .infinity, alignment: .leading)  // << fix 1 !!
    .background(Color.red)
    .padding(.leading, 16)
    .padding(.trailing, 16)

gives:

demo

At second SwiftUI Text does not allow orphan words. See next for details https://stackoverflow.com/a/71698509/12299030

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading