gpt4 book ai didi

swiftui - 使文本元素始终为 "two lines"

转载 作者:行者123 更新时间:2023-12-04 07:44:25 27 4
gpt4 key购买 nike

我有一个像这样的垂直堆栈

VStack
Text(lineA)
.lineLimit(2)

Text(lineB)

Text(lineC)
}

别问我为什么。我的老板决定他们希望第一个 Text 始终包含两行,即使 lineA 变量没有字符使文本增长到两行。

有没有办法让第一个文本在两行时渲染,并且在多于两行时仍然显示沉默?

我可以看到使用框架来做到这一点,一种自杀,将高度固定为一个可能因设备而异的点值。

有没有更致命的解决方案?

最佳答案

这是可能的解决方案(使用 Xcode 12.4/iOS 14.4 测试)

var body: some View {
VStack {
Text("X\nX").foregroundColor(.clear)
.frame(maxWidth: .infinity)
.overlay(
Text("sdfasdfasdfasd asd f asdf asd f asdf asd fas df ")
// Text("sdfasdfasdfasd asd f asdf asd f asdf asd fas df asdf asd fa sdf asdfasdfasd asdf as df asdfasdf asdf")
, alignment: .top)

Text("lineB")
Text("lineC")
}
}

enter image description here

enter image description here

关于swiftui - 使文本元素始终为 "two lines",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67269159/

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