Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI adjustsFontSizeToFitWidth equivalent

I'm building an app in SwiftUI which contains a lot of components, including a Text view which contains a long string.

When I launch the app on iPhone 11 everything works perfectly, but when I launch it on a smaller device (iPhone SE for example), the Text view cuts the text in the middle, and shows three dots (...) instead of the rest of the text.

What would be the best solution for this? Is there an equivalent for UILabel's adjustsFontSizeToFitWidth in SwiftUI?

Thank you!

like image 281
F.SO7 Avatar asked Dec 11 '25 08:12

F.SO7


2 Answers

Not exactly, but kind of...

Text("Your long next here")
   .minimumScaleFactor(0.2) // value is up to you
like image 176
Asperi Avatar answered Dec 12 '25 22:12

Asperi


You can use a really large font size and use func minimumScaleFactor(_ factor: CGFloat) to let the text shrink. You can also add lineLimit(1) to force the text to be on one line.

Text("My long text adjusted to fit width")
    .font(.system(size: 200))
    .minimumScaleFactor(0.0001)
    .lineLimit(1)
like image 30
Nicolas Mandica Avatar answered Dec 12 '25 23:12

Nicolas Mandica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!