小编Rob*_*son的帖子

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 …
Run Code Online (Sandbox Code Playgroud)

text styles button swiftui

4
推荐指数
1
解决办法
2868
查看次数

标签 统计

button ×1

styles ×1

swiftui ×1

text ×1