I have a HighlightedText View which is initialized with a string, a default font and a highlight font. The string can have markers to indicate which portions of it need to be highlighted. That all works fine. Now, instead of initialize it with a default font and a highlight font, I would like to be able to write this in a more swift-ui way to allow for more flexibility and improve readiness. So I would like to be able to do something like this:
HighlightedText("My text <highlighted>")
.defaultFont(.body)
.highlightFont(.title)
I know the standard way should be using a ViewModifier, however all I get from within its body function is a Content type and there doesn't seem to be a way I could cast it into my HighlightedText view and configure it as needed. All I seem to be able to do from within its body is just call other modifiers from View protocol, but that's not enough for my use case.
I've tried this extension, where defaultFont is a file private @State property defined in HighlightedText:
extension HighlightedText {
func defaultFont(_ font: Font) -> some View {
defaultFont.font = font
return body
}
}
That however, does not work. The default font I pass over never gets applied.