Reputation: 1012
I am trying to display multiple Text(string) in a single sentence as below, but Its breaks the line.
struct DisplayView: View {
@State var stringList = [
"Yes, it only returns the first occurrence. Therefore, to fulfill our purpose, ",
"we need to construct a while loop to iterate all the occurrences of a given string within the receiver. A reference snippet will be like the following code block. ",
"Note that an offset is also added to reduce the length of range in each iteration. So, actually, the position of each loop is basically based on the upper bound of the previous iteration"
]
var body: some View {
VStack {
ForEach(0..<stringList.count) { index in
Text("\(stringList[index])")
}
}
}
}
Here the issue is after each Text, it breaks the line.
But It works like-
struct DisplayView: View {
@State var stringList = [
"Yes, it only returns the first occurrence. Therefore, to fulfill our purpose, ",
"we need to construct a while loop to iterate all the occurrences of a given string within the receiver. A reference snippet will be like the following code block. ",
"Note that an offset is also added to reduce the length of range in each iteration. So, actually, the position of each loop is basically based on the upper bound of the previous iteration"
]
var body: some View {
VStack {
Text("\(stringList[0])") + Text("\(stringList[1])") + Text("\(stringList[2])")
}
}
}
But the point is list contains any number of strings 1/4/6/10. It could be multiple.
I want output like this. All text should continue after the previous one ends.
Please help. Thanks in advance.
Upvotes: 1
Views: 310
Reputation: 10385
SwiftUI.Text
can be appended to each other with the plus operator, so you can concatenate the text like so:
struct DisplayView: View {
@State var stringList = [
"Yes, it only returns the first occurrence. Therefore, to fulfill our purpose, ",
"we need to construct a while loop to iterate all the occurrences of a given string within the receiver. A reference snippet will be like the following code block. ",
"Note that an offset is also added to reduce the length of range in each iteration. So, actually, the position of each loop is basically based on the upper bound of the previous iteration"
]
var body: some View {
stringList.reduce(Text(""), { txt, str in txt + Text(str) })
}
}
Upvotes: 3
Reputation: 3307
You should be able to achieve this by concatenating the strings and placing them in the same Text
view rather than creating additional views:
struct DisplayView: View {
@State var stringList = [
"Yes, it only returns the first occurrence. Therefore, to fulfill our purpose, ",
"we need to construct a while loop to iterate all the occurrences of a given string within the receiver. A reference snippet will be like the following code block. ",
"Note that an offset is also added to reduce the length of range in each iteration. So, actually, the position of each loop is basically based on the upper bound of the previous iteration"
]
var body: some View {
Text(stringList.joined(separator: ""))
}
}
Upvotes: 0