I'm facing the following problem with SwiftUI Text: In the following example SwiftUI breaks the word "Amazement" into "amazeme" on the first line and "nt" on the second. How to avoid it, isn't it a bug?
I want the word "amazement" to be written on one line. Is there any modifier that can allow this (don't divide words or something)?
Tried .allowsTightening, .fixedSize. Changed the order of modifiers, Didn't help.
Is it a bug or we currently don't have an option to fix this? The solution should work for every String, not only for the mentioned string.
You can replicate the behaviour using this code:
struct TestView2: View {
var body: some View {
ZStack {
Text("Amazement Awaits us at every corner")
.font(.system(size: 160))
.foregroundColor(.blue)
.foregroundColor(.white)
.lineLimit(4)
.multilineTextAlignment(.leading)
.minimumScaleFactor(0.01)
//.allowsTightening(true)
//.fixedSize(horizontal: false, vertical: true)
}
}
}
struct TestView2_Previews: PreviewProvider {
static var previews: some View {
TestView2()
}
}


Looks like wrapping words by character is the default when it comes to fitting text in a view. I found that
.minimumScaleFactoronly works if the system is not able to fit the text (with character breaks). If that didn't work then it will try to scale the text down. If you try to decrease thelinelimitthenminimumScaleFactorwill kick in and try to make the text smaller until it fits.At the moment I'm using a hack where I check the number of characters of words in the text, if any word has more characters than a certain threshold then I decrease the font size, otherwise I use the default size.
This won't work for all scenarios, but hopefully it's tweakable until Apple comes up with a proper solution to this as I can't imagine a scenario where breaking words by character without even a hyphen is the best solution.