Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created June 27, 2020 11:52
Show Gist options
  • Save sturdysturge/3e622876ba677bcf77982c73b1803b37 to your computer and use it in GitHub Desktop.
Save sturdysturge/3e622876ba677bcf77982c73b1803b37 to your computer and use it in GitHub Desktop.
BluePaddingModifier
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Blue")
.foregroundColor(.blue)
.padding(10)
Text("Blue")
.modifier(BluePaddingModifier(padding: 10))
Text("Blue")
.blue(10)
Text("Blue")
.b(10)
}
}
}
struct BluePaddingModifier: ViewModifier {
let padding: CGFloat
func body(content: Content) -> some View {
content
.foregroundColor(.blue)
.padding(padding)
}
}
extension View {
func blue(_ padding: CGFloat) -> some View {
let modifier = BluePaddingModifier(padding: padding)
return ModifiedContent(content: self, modifier: modifier)
}
func b(_ padding: CGFloat) -> some View {
self.modifier(BluePaddingModifier(padding: padding))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment