I am new to SwiftUI and trying to understand how I can combine multiple text views that can end up being a different view type because of setting the background color, as an example. Below is my current code and what I am currently achieving. This works, however using HStack it doesn’t display the text left to right correctly.
I would rather just combine all the text views if possible but could not figure out how to do that since some text views end up as “any View”. I have also tried modifiers but it never seems to apply the background color.
Thanks!
private func getTexts() -> some View {
HStack(alignment: .center, spacing: 5) {
let words = note.text.components(separatedBy: " ")
ForEach(words.indices, id: .self) { index in
let currentWord = words[index]
if (currentWord.lowercased() == "Reading".lowercased()) {
Text(currentWord)
.padding(.all, 4)
.background(.yellow)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
}
else {
Text(currentWord)
}
}
}
}
When outputting just one line on the screen with the code above I get the result shown below:
I want to achieve the picture above but with multi-line text:
2