如何使 VStack 的宽度根据文本的宽度增长
How to make the width of a VStack grow based on the width of a Text
我需要的是能够创建一个 VStack
,如果 Text
内部增长,它的宽度也会增长。如果可能的话,我还希望能够将 VStack
最小宽度保持在 180pt
。
除了没有最小宽度之外,下面的代码几乎可以满足我的需要;我可以没有它。下面代码的问题是当我在设备上测试它时收到紫色警告。
知道为什么我会收到以下警告吗?
再见,我没有收到任何错误,只有警告。
struct GeneralTest: View {
var body: some View {
VStack{
Text("244GGDD")
.frame(width: .infinity, height: 55, alignment: .center)
.font(Font.system(size: 42, weight: .light))
.foregroundColor(Color.blue)
.padding(.leading, 20)
.padding(.trailing, 20)
}
.frame(width:.infinity, height: 100, alignment: .center)
.background(Color.gray)
}
}
警告:
以下修饰符应该可以满足您的需求:
Text("244GGDD")
.fixedSize(horizontal: false, vertical: true)
.frame(minWidth: 180)
// other modifiers
VStack
应包裹整个文本,宽度至少为 180 磅。
我需要的是能够创建一个 VStack
,如果 Text
内部增长,它的宽度也会增长。如果可能的话,我还希望能够将 VStack
最小宽度保持在 180pt
。
除了没有最小宽度之外,下面的代码几乎可以满足我的需要;我可以没有它。下面代码的问题是当我在设备上测试它时收到紫色警告。
知道为什么我会收到以下警告吗?
再见,我没有收到任何错误,只有警告。
struct GeneralTest: View {
var body: some View {
VStack{
Text("244GGDD")
.frame(width: .infinity, height: 55, alignment: .center)
.font(Font.system(size: 42, weight: .light))
.foregroundColor(Color.blue)
.padding(.leading, 20)
.padding(.trailing, 20)
}
.frame(width:.infinity, height: 100, alignment: .center)
.background(Color.gray)
}
}
警告:
以下修饰符应该可以满足您的需求:
Text("244GGDD")
.fixedSize(horizontal: false, vertical: true)
.frame(minWidth: 180)
// other modifiers
VStack
应包裹整个文本,宽度至少为 180 磅。