Something like this wouldn't work for you?
struct SimpleTestView: View {
@State private var viewColor: Color = .green
var body: some View {
VStack(spacing: 20) {
(Text("Hello ") +
Text(Image(systemName: "a.circle")).foregroundColor(viewColor) +
Text(" World!"))
.font(.largeTitle)
Button(action: {
withAnimation {
viewColor = viewColor == .green ? .purple : .green
}
}) {
Text("Change color")
}
}
}
}
struct SimpleTestView_Previews: PreviewProvider {
static var previews: some View {
SimpleTestView()
}
}
Even if you don't want to use SFSymbols for the letter in a circle image, you could probably accomplish the same thing by applying .withRenderingMode(.alwaysTemplate)
to a UIImage
and then build your SwiftUI Image
from there.
But you know your code so maybe this won't work. But I wanted to throw it out there just in case.
Hmm, on second thought this doesn't seem to be animating, despite the withAnimation
block.
So do you need it to simply change color like in the first example, or actually to animate like in this example?
This one was done by setting the foregroundColor
to .white
and then using the colorMultiply()
modifier with the intended color inside, like so:
struct SimplerTestView: View {
@State private var color = Color.red
var body: some View {
Text("Animate Me!")
.font(.largeTitle)
.foregroundColor(Color.white)
.colorMultiply(color)
.onTapGesture {
withAnimation(.easeInOut(duration: 1)) {
color = color == .red ? .blue : .red
}
}
}
}
But it won't work when concatenating Text
s together since it changes the View
's type from Text
.