SwiftUI - 文本 minimumScaleFactor 仅在需要时才缩放

A. *_*cas 10 scaling text view scale swiftui

我有一个文本,.minimumScaleFactor(0.1)当其字符串的长度延伸到视图之外时,我想用 a 修改它。但是,缩放每次都适用,即使原始字体大小非常好。

我的观点是这样构建的:

        VStack(alignment: .center, spacing: 0) {
            HStack {
                Image("medal \(place)").resizable()
                    .foregroundColor(color)
                    .frame(width: 40, height: 40)
                Spacer()
                Text(username)
                    .font(.bold(16))
                    .lineLimit(1)
                    .minimumScaleFactor(0.1)
                    .frame(alignment: .trailing)
                    .foregroundColor(Color("mediumTextColor"))
            }
            Spacer()
            Text(score)
                .font(.extraBold(60))
                .foregroundColor(color)
                .lineLimit(1)
                .minimumScaleFactor(0.7)

            Spacer()
        }
        .frame(height: 96)
        .padding(10)
        .cornerRadius(16)
        .overlay(RoundedRectangle(cornerRadius: 16)
        .stroke(color, lineWidth: 2))
Run Code Online (Sandbox Code Playgroud)

mis*_*may 6

添加.lineLimit(1)到文本中效果会很好。

Xcode:11.3.1


Ale*_*der 5

继续上面的回答。

重要的是应用修饰符的顺序:

Text("This is a really long sentence.") 
   .minimumScaleFactor(0.5)                
   .font(.custom("OpenSans", size: 15))
   .lineLimit(1)
   .layoutPriority(1)
Run Code Online (Sandbox Code Playgroud)

不需要 GeometryReader 除非您需要它来做其他事情


A. *_*cas 3

在我的两个文本视图所在的 VStack 周围添加一个 GeometryReader 解决了这个问题。根据@gohnjanotis 的要求:

GeometryReader { proxy in
    VStack(alignment: .center, spacing: 0) {
        HStack {
            Image("medal \(self.place)").resizable()
                .foregroundColor(self.color)
                .frame(width: 40, height: 40)

            Spacer()
            Text(self.username)
                .minimumScaleFactor(0.1)
                .font(.bold(16))
                .lineLimit(1)
                .frame(alignment: .trailing)
                .foregroundColor(Color("mediumTextColor"))
                .layoutPriority(1)

        }
        .padding(.top, 10)
        .padding(.horizontal, 10)
        Spacer()
        Text(self.score)
            .font(.extraBold(60))
            .foregroundColor(self.color)
            .lineLimit(1)
            .minimumScaleFactor(0.1)
            .layoutPriority(1)
            .padding(.horizontal, 10)
            .padding(.bottom, 10)
            .offset(y: -10)
    }
    .frame(width: proxy.size.width, height: proxy.size.height, alignment: .top)
}
.frame(maxHeight: 130)
Run Code Online (Sandbox Code Playgroud)