I'm trying to construct a vertical grid of strings that have variable lengths to be displayed side by side to each other (such as a collection of social media tags), as follows:
struct ContentView: View {
let data = ["first text",
"second text",
"long text for testing",
"short",
"some medium text"
]
let gridItem = GridItem(.adaptive(minimum: 40, maximum: 300))
var body: some View {
ScrollView {
LazyVGrid(columns: [gridItem]) {
ForEach(data, id:\.self) { item in
Text(item)
.lineLimit(1)
.background(Color.red)
}
}
}
}
}
but the problem with LazyVGrid
is that it's tied to the minimum item width defined in its initializer which doesn't allow it to dynamically adapt to the width of each item.
The result for the above code is:
My question is: how can I display any dynamic list of variable-length strings in a vertically scrollable view in SwiftUI ?