The issue I am encountering is, I need to be able to create a view in SwiftUI that looks like a sentence of text but has blanks where the user can enter their own text. I also need a way to be able to capture the entered text from the binding, but not sure how to do multiple bindings in a loop.
Ive tried a few options and the closest I have got is using LazyGrid, but it doesn't look like a normal sentence due to spacing?
Any help appreciated.
struct MultiText: View {
@State var text = "There was once a {-} that only had {-} in it. How did they get in? {-}"
@State var answerText: String = "XXXXXXX"
let rows = [
GridItem(.adaptive(minimum: 80))
]
var body: some View {
GeometryReader { geo in
HStack {
LazyVGrid(columns: rows, alignment: .leading, spacing: 0) {
ForEach(text.components(separatedBy: " "), id: \.self) { component in
if component == "{-}" {
TextField("", text: $answerText
)
} else {
Text(component)
}
}
}.frame(width: geo.size.width * 0.80)
Image(systemName: "flag").frame(width: 30, height: 30)
}.frame(width: geo.size.width)
}
}
}
This produces the following result