Skip to content

Instantly share code, notes, and snippets.

@sebj
Created July 22, 2023 20:01
Show Gist options
  • Save sebj/032c135bf4de5fbe82e58de8165b565f to your computer and use it in GitHub Desktop.
Save sebj/032c135bf4de5fbe82e58de8165b565f to your computer and use it in GitHub Desktop.
Example: define dynamic colors via SwiftUI's ShapeStyle on iOS 17
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.foregroundStyle(.customBackground)
}
}
#Preview {
ContentView()
}
// MARK: - Custom Colors
extension ShapeStyle where Self == CustomBackgroundColor {
static var customBackground: CustomBackgroundColor { .init() }
}
struct CustomBackgroundColor: ShapeStyle {
func resolve(in environment: EnvironmentValues) -> some ShapeStyle {
if environment.colorScheme == .light {
return Color.red
} else {
return Color.green
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment