SwiftUI:调整文本大小以适合按钮

Rob*_*son 4 text styles button swiftui

我正在根据数据库中的值更新按钮文本,由于这些值可能很长或很短,我希望按钮大小保持不变,并且文本根据值的大小动态调整大小,因此它保持在按钮并且不被截断(值将是句子,它是一个语言学习应用程序)。我基本上想给字体大小一个最小可能值和最大可能值。

我知道如何在 Android Studio 中执行此操作。

autoSizeMinTextSize=14sp
autoSizeMaxTestSize=20sp
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用诸如带有minimumScaleFactor的大字体大小之类的建议时,它不起作用。它只会使文本变得非常小。

这是我的按钮样式。

struct SoundButton: ButtonStyle {

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .foregroundColor(Color.black)
            .multilineTextAlignment(.center)
            .font(.custom("arial", size: 20))
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(configuration.isPressed ? LinearGradient(gradient: Gradient(colors: [Color("DarkestGreen"), Color("LightGreen")]), startPoint: .top, endPoint: .bottom):LinearGradient(gradient: Gradient(colors: [Color("DarkGreen"), Color("LightGreen")]), startPoint: .top, endPoint: .bottom))
            .cornerRadius(10)
            .shadow(radius: 3.0)
            .overlay(
                RoundedRectangle(cornerRadius: 10)
                    .stroke(Color.white, lineWidth:2))
            .padding(2)
            .scaleEffect(configuration.isPressed ? 0.975 : 1.0)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是按钮代码。

                        Button(action: {
                            playSound(sound: "\(self.category)_audio1", type: "mp3")
                            self.translation = self.btns[1][1]
                        }) {
                            if(gotValues==true) {
                                Text("\(self.btns[1][0])")
                            } else {
                                Text("\(self.btn1)")
                            }
                        }
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序屏幕的样子。

在此输入图像描述

我可以添加什么来达到预期的效果?谢谢。

另外,这是可用的 Android 版本。

在此输入图像描述

Asp*_*eri 5

据我了解您的目标,以下内容应该会给您带来所需的效果(或某种程度的效果)......

使用 Xcode 11.4 / iOS 13.4 进行测试

演示

configuration.label
    .padding(.horizontal, 8).lineLimit(1).minimumScaleFactor(0.4) // << this place !
    .foregroundColor(Color.white)
Run Code Online (Sandbox Code Playgroud)