Reputation: 5480
In SwiftUI
, you can combine Text
objects like so:
var body: some View {
Text("You can") + Text(" add lots of objects that wrap normally")
}
This gives the benefit of having multiple Text objects that act as one, meaning they are on the same line and wrap appropriately.
What I can't figure out is how to combine n number of Text
objects, based on an array or really any way to increment.
I've tried ForEach
like so,
var texts = ["You can", " add lots of objects that wrap normally"]
var body: some View {
ForEach(texts.identified(by: \.self)) {
Text($0)
}
}
But that looks like this
when I want it to look like this
Could anyone show me how this is done?
Use case: Styling part of a Text object and not the other parts. Like what is possible with NSMutableAttributedString
Upvotes: 1
Views: 184
Reputation: 535231
You seem to be describing something like this:
var body: some View {
let strings = ["manny ", "moe", " jack"]
var texts = strings.map{Text($0)}
texts[1] = texts[1].underline() // illustrating your use case
return texts[1...].reduce(texts[0], +)
}
However, I think it would be better to wait until attributed strings arrive.
Upvotes: 3