SwiftUI:minimumScaleFactor 没有均匀地应用于堆栈元素 [英] SwiftUI: minimumScaleFactor not applying evenly to stack elements

查看:25
本文介绍了SwiftUI:minimumScaleFactor 没有均匀地应用于堆栈元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本的水平堆栈(第二个以蓝色突出显示).它非常适合 iPhone XR,但是在较小的设备(如 iPhone X)上时,文本不适合.我试图通过使用 minimumScaleFactor 来缩放文本来解决这个问题.然而,SwiftUI 似乎决定在堆栈中扩展什么.在此示例中,在较小的设备上,它会删除粗体并仅缩小第一个(非蓝色)元素.蓝色元素保持不变.关于为什么会发生这种情况的任何想法?如何一起缩小粗体文本元素的大小?谢谢!

I have a horizontal stack of two pieces of text (the second highlighted in a blue). It fits fine on an iPhone XR, however when on a smaller device (like iPhone X), the text doesn't fit. I attempted to solve this by using minimumScaleFactor to scale the text. However, SwiftUI seems to make decisions on what to scale in the stack. In this example, on the smaller device, it removes the bolding and shrinks the first (non-blue) element only. The blue element remains unchanged. Any ideas as to why this would happen? How can I scale both bolded text elements down in size together? Thanks!

    var normalText: String
    var highlightedText: String

    var body: some View {
        HStack() {
            Text(normalText)
                .font(.largeTitle)
                .bold()
                .lineLimit(1)

            Text(highlightedText)
                .font(.largeTitle)
                .bold()
                .lineLimit(1)
                .foregroundColor(.blue)

            Spacer()
        }
        .minimumScaleFactor(0.5)
    }
}

以下是它在较小设备上的显示方式:以及它如何在更大的设备上显示:

Here is how it displays on a smaller device: And how it shows on a larger device:

推荐答案


struct ContentView: View {
    var normalText: String = "Hello and Welcome to Stack "
    var highlightedText: String = "Overflow"

    var body: some View {
        HStack() {
            Group {
                Text(normalText).bold() +
                Text(highlightedText).bold()
                .foregroundColor(.blue)
            }
            .lineLimit(1).font(.largeTitle)

            Spacer()
        }
        .minimumScaleFactor(0.5)
    }
}

+ 是为 2 个文本而不是 2 个视图定义的,并且 lineLimit() 返回一个视图,需要对连接的文本进行分组.我把组合的字符串加长以迫使它收缩.

+ is defined for 2 Text's but not 2 View's and as lineLimit() returns a View the joined text's needed to be Grouped. I made the combined string longer to force it to shrink.

这篇关于SwiftUI:minimumScaleFactor 没有均匀地应用于堆栈元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆