SwiftUI Text.minimumScaleFactor(…)无法正确缩放文本?

问题描述 投票:0回答:1

在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")
        }
    }
}

SwiftUI preview

text swiftui scale swiftui-bug
1个回答
0
投票

我遵循了此命令

  • 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这样的字体大小强制打破这种缩放比例,您可能可以实现:)

© www.soinside.com 2019 - 2024. All rights reserved.