In my Watch app on watchOS 9 I was using .foregroundColor(myColour)
to colour the text in a widgetLabel on a corner complication like this:
let myColour: Color = functionThatReturnsAColorObjectConstructedLike Color.init(...) // green
.widgetLabel {
Text(myText)
.foregroundColor(myColour)
}
It worked fine; the widget label was green.
Now, in watchOS 10, I see that foregroundColor()
is being deprecated in favour of foregroundStyle()
, and I can use .foregroundStyle(.green)
, and - importantly - foregroundStyle()
is only available on watchOS 10 and newer.
myColour
is calculated depending on some other info, so I can't just write .green
, and when I use .foregroundStyle(myColour)
the widget label comes out as white every time, even if I set myColour = .green
.
I think I have to use some sort of extension to pick the right combination, something like:
extension View {
func foregroundType(colour: Colour, style: any ShapeStyle) -> some THING? {
if #available(watchOS 10.0, *) {
return foregroundStyle(style)
} else {
return foregroundColor(colour)
}
}
}
// Usage
let myStyle: any ShapeStyle = SOMETHING?
...
.widgetLabel {
Text(myText)
.foregroundType(colour: myColour, style: myStyle)
It doesn't work. I just can't figure out what should be returned, nor how to return it. Any ideas?