gpt4 book ai didi

swiftui - 在 SwiftUI 中使用 aspectRatio 的方形文本

转载 作者:行者123 更新时间:2023-12-04 11:55:58 24 4
gpt4 key购买 nike

我正在尝试使用 Swift UI 实现以下布局……

enter image description here

struct ContentView : View {
var body: some View {
List(1...5) { index in

HStack {
HStack {
Text("Item number \(index)")
Spacer()
}.padding([.leading, .top, .bottom])
.background(Color.blue)

Text("i")
.font(.title)
.italic()
.padding()
.aspectRatio(1, contentMode: .fill)
.background(Color.pink)

}.background(Color.yellow)
}
}
}

我想要 Text("i")为方形,但设置 .aspectRatio(1, contentMode: .fill)好像什么都没做……

enter image description here

我可以将文本的框架宽度和高度设置为正方形,但似乎设置纵横比应该以更动态的方式实现我想要的。

我错过了什么?

最佳答案

我认为这就是你要找的:

List(1..<6) { index  in
HStack {
HStack {
Text("Item number \(index)")

Spacer()
}
.padding([.leading, .top, .bottom])
.background(Color.blue)

Text("i")
.font(.title)
.italic()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.aspectRatio(1, contentMode: .fill)
.background(Color.pink)
.fixedSize(horizontal: true, vertical: false)
.padding(.leading, 6)
}
.padding(6)
.background(Color.yellow)
}
答案是,我不建议给 SwiftUI 太多的自由来决定大小。现在最大的 SwiftUI 问题之一是它决定如何使 View 相互适应的方式。如果 SwiftUI 方面出现问题,可能会导致对 UIKit 的 sizeToFit 方法的调用过多,这可能会减慢应用程序的速度,甚至使其崩溃。
但是,如果您在几种不同的情况下尝试过这个解决方案并且它有效,您可以假设在您的情况下,让 SwiftUI 选择决定大小是没有问题的。

关于swiftui - 在 SwiftUI 中使用 aspectRatio 的方形文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56773329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com