gpt4 book ai didi

swiftui - SwiftUI 中形状的动态高度

转载 作者:行者123 更新时间:2023-12-04 15:32:12 28 4
gpt4 key购买 nike

这是我想做的一个最小的、可重复的例子。

我有一系列的段落。

var notes = [
"One line paragraph",
"This is a small paragraph. This is a small paragraph. This is a small paragraph. This is a small paragraph.",
"This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph."
]

这就是我想要显示的方式:
enter image description here

这是我的代码。
struct ContentView: View {
var notes = [
"One line paragraph",
"This is a small paragraph. This is a small paragraph. This is a small paragraph. This is a small paragraph.",
"This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph. This is a big paragraph."
]

var body: some View {
VStack(alignment: .leading) {
ForEach(self.notes, id: \.self) {note in
HStack {
Capsule()
.fill(Color.blue)
.frame(width: 4.5)

Text(note)
.lineLimit(nil)
}
.padding()
}

}
}
}

这是我得到的输出:
enter image description here

我什至尝试添加 Spacer()到底部 VStack (在 ForEach 之后)但输出仍然相同。

我想知道的是 如何将这些蓝色竖条的高度更改为其各自段落的高度 就像在第一个屏幕截图中一样。

最佳答案

这是可能的方法。使用 Xcode 11.4/iOS 13.4 测试

demo

var body: some View {
VStack(alignment: .leading) {
ForEach(self.notes, id: \.self) {note in
HStack {
Text(note)
.padding(.leading)
}
.overlay(Capsule() // also can be .background
.fill(Color.blue)
.frame(width: 4.5), alignment: .leading)
.padding()
}
}
}

关于swiftui - SwiftUI 中形状的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61027919/

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