In SwiftUI, I'm trying to use minimumScaleFactor
to adapt Text
size automatically. It looks like it has curious behaviors. For ex, it works with usual characters but fails with accented characters. I'm searching for a solution without arbitrary ratio and that works with all fonts.
Here is a simplified example showing the actual behavior vs the expected behavior.
What am I doing wrong?
import SwiftUI
struct TestScaleView: View {
let text: String
var body: some View {
GeometryReader { geo in
Text(self.text)
.font(.system(size: 1000))
.minimumScaleFactor(.leastNonzeroMagnitude)
.lineLimit(1)
.frame(width: geo.size.width, height: geo.size.height)
}
}
}
struct TestNoScaleView: View {
let text: String
var fontSize: CGFloat
var body: some View {
GeometryReader { geo in
Text(self.text)
.font(.system(size: self.fontSize))
.lineLimit(1)
.frame(width: geo.size.width, height: geo.size.height)
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
Group {
// WHAT HAPPENS
TestScaleView(text: "This shall scale perfectly!")
.previewLayout(.fixed(width: 500, height: 70))
.previewDisplayName("This is acceptable but not perfect")
TestScaleView(text: "This shall scale perfectly with special characters like é or @!")
.previewLayout(.fixed(width: 500, height: 70))
.previewDisplayName("Ouch.")
TestScaleView(text: "Small")
.previewLayout(.fixed(width: 500, height: 70))
.previewDisplayName("Not working well, right?")
// WHAT IS EXPECTED
TestNoScaleView(text: "This shall scale perfectly!", fontSize: 46)
.previewLayout(.fixed(width: 500, height: 70))
.previewDisplayName("Adjusted manually")
TestNoScaleView(text: "This shall scale perfectly with special characters like é or @!",
fontSize: 18)
.previewLayout(.fixed(width: 500, height: 70))
.previewDisplayName("Adjusted manually")
TestNoScaleView(text: "Small",
fontSize: 94)
.previewLayout(.fixed(width: 500, height: 70))
.previewDisplayName("Adjusted manually")
}
}
}