SwiftUI Text.minimumScaleFactor(…)无法正确缩放文本?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SwiftUI Text.minimumScaleFactor(…)无法正确缩放文本?相关的知识,希望对你有一定的参考价值。
在SwiftUI中,我正在尝试使用minimumScaleFactor
来自动调整Text
的大小。看起来它具有奇怪的行为。例如,它适用于普通字符,但不适用于带重音符号的字符。我正在寻找没有任意比率的解决方案,并且该解决方案适用于所有字体。
这是显示实际行为与预期行为的简化示例。
我在做什么错?
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")
}
}
}
答案
我遵循了此命令
- lineLimit
- font
- minimumScaleFactor
似乎效果很好。
VStack {
Text("This shall scale perfectly!")
.lineLimit(1)
.font(.system(size: 100))
.minimumScaleFactor(.leastNonzeroMagnitude)
Divider()
Text("This may okay to be truncated from the middle, I am adding more text!!")
.truncationMode(.middle)
.lineLimit(1)
.font(.system(size: 40))
.minimumScaleFactor(0.5)
Divider()
Text("This shall scale perfectly with special characters like é or @!")
.lineLimit(1)
.font(.system(size: 38))
.minimumScaleFactor(.leastNonzeroMagnitude)
Divider()
Text("Small")
.lineLimit(1)
.font(.system(size: 60))
.minimumScaleFactor(0.1)
}
结果
顺便说一句,如果您想用1000这样的字体大小强制打破这种缩放比例,您可能可以实现:)
以上是关于SwiftUI Text.minimumScaleFactor(…)无法正确缩放文本?的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI NavigationLink 如何到达另一个 SwiftUI 页面?
SwiftUI - 如何在 SwiftUI 中弹出到特定视图?