Interpolating Text views in SwiftUI
I often find myself using an HStack
to combine some text and an image. Something like:
HStack {
Text("Go for a walk")
Image(systemName: "shoes.2")
}
This can be streamlined a bit though by interpolating the Image
within the Text
view:
Text("Go for a walk \(Image(systemName: "shoe.2"))")
Where I think this really shines is when you want to modify segments of a string, while still applying some view modifiers to the string as a whole:
Text("A \(Text("big").font(.largeTitle)) color \(Text("change").foregroundStyle(.green))")
.font(.caption)
.lineLimit(2)
.frame(width: 80)