UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Stop Text() being truncated

Forums > SwiftUI

Hi everybody

I've come accross a very annoying behaviour which I couldn't fix yet... On iPhone 11 Pro (real device, Xcode preview or in the simulator) the code below is always truncating the "Restart" Text, i.e. it always shows "Rest...".

As soon as I remove .font(.headline) from the first Text, "Restart" is shown completly... If "Restart" has a slightly bigger font than .body and any horizontal padding, it is getting truncated due to the font of the previous text. But as we see, the first line of text is a lot wider thant the second

Can somebody confirm this behaviour? And is there an explanation?

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Draw game: Nobody wins")
                .font(.headline)

            Text("Restart")
                .font(.title)
                .padding()
        }
    }
}

4      

That is interesting behavior. You would certainly think there would be enough room in the VStack to accommodate the Restart text. While I can't entirely explain the reason, I can offer a solution if it helps.

If you add two modifiers to your "Restart" text to force it to scale down in size, then the chance of it truncating will be minimized. See my example below... I hope this helps.

  • Dan

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Draw game: Nobody wins")
                .font(.headline)

            Text("Restart")
                .lineLimit(1)
                .minimumScaleFactor(0.5)
                .font(.title)
                .padding()
        }
    }
}

9      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.