We can't call this method directly. It's is a workaround to concatenate Texts. Originally it produces "hello %@ %@" and it accepts a fixed amount of objects. Text has its own .foregroundStyle modifier and it's not erasing type, while Image's .foregroundStyle is returning "some View". For testing this, you can use Image's modifier .renderingMode(_:), in example. It will return Image and all will be formatted correctly.
But it’s messy. I would like to transform my symbol from an image to a character, because sf symbols are semantically equivalent to characters (including emoji) - graphic units that maintain recognizable identity across different visual renderings."
It's the only way to keep interpolation restrictions currently. Other way is to shift to H(V)Stack but it's already a design change. SF symbols are close to chars but still more close to Image.
Great article. But I can’t get my head why I can’t use this technique with other views.
Text("hello \(Text("world").foregroundStyle(.blue)) \(Image(systemName: "diamond"))").foregroundStyle(.red) // ✅
Image(systemName: "diamond")
.foregroundStyle(.green) ✅
Text("hello \(Text("world").foregroundStyle(.blue)) \(Image(systemName: "diamond").foregroundStyle(.green))") ❌
Alan, that's a good question. All circles around: https://developer.apple.com/documentation/swiftui/localizedstringkey/stringinterpolation/appendinterpolation(_:)
We can't call this method directly. It's is a workaround to concatenate Texts. Originally it produces "hello %@ %@" and it accepts a fixed amount of objects. Text has its own .foregroundStyle modifier and it's not erasing type, while Image's .foregroundStyle is returning "some View". For testing this, you can use Image's modifier .renderingMode(_:), in example. It will return Image and all will be formatted correctly.
Thanks Anton, so I can achieve my goal with:
Text("\(Text("hello").foregroundStyle(.red)) \(Text("world").foregroundStyle(.blue)) \(Image(systemName: "diamond").renderingMode(.template))").foregroundStyle(.primary)
But it’s messy. I would like to transform my symbol from an image to a character, because sf symbols are semantically equivalent to characters (including emoji) - graphic units that maintain recognizable identity across different visual renderings."
It's the only way to keep interpolation restrictions currently. Other way is to shift to H(V)Stack but it's already a design change. SF symbols are close to chars but still more close to Image.