Multiline text in SwiftUI
02 Jul 2020
I keep running into an issue in SwiftUI where some text truncates where I don’t expect it to. Here’s an example from Personal Best’s onboarding screen:
I’m sure that I’m doing something wrong here, or perhaps misunderstanding how SwiftUI’s layout system works. But for now, I made a modifier that I can apply to stop this from happening:
// View modifier
struct Multiline: ViewModifier {
func body(content: Content) -> some View {
content.fixedSize(horizontal: false, vertical: true)
}
}
// Using it
Text("Hello world").modifier(Multiline())
One day I’ll figure out the layout system a bit more so I won’t need to do this, but for now it’s doing the trick for me.
Thanks for reading.
To get in touch, email me or find me on Mastodon or Twitter.
If you liked this post, you'll love my iOS apps. Check them out below.