Skip to content

Instantly share code, notes, and snippets.

@otaviocc
Last active March 13, 2021 15:11
Show Gist options
  • Save otaviocc/fb6a67ca824b20eacfba9c116560bf2f to your computer and use it in GitHub Desktop.
Save otaviocc/fb6a67ca824b20eacfba9c116560bf2f to your computer and use it in GitHub Desktop.
struct TitleTextStyle: TextStyle {
func makeBody(
_ configuration: TextStyleConfiguration
) -> some View {
configuration.text
.font(.title)
.fontWeight(.bold)
.foregroundColor(.accentColor)
}
}
// ...
Text(title)
.textStyle(TitleTextStyle())
protocol TextStyle {
associatedtype Body: View
func makeBody(
_ configuration: TextStyleConfiguration
) -> Body
}
struct TextStyleConfiguration {
let text: Text
}
extension Text {
func textStyle<S>(
_ style: S
) -> some View where S: TextStyle {
let configuration = TextStyleConfiguration(
text: self
)
return style.makeBody(configuration)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment