Created
June 24, 2020 23:34
-
-
Save 0xLeif/316bf7d6db041c10086f457bb0c26172 to your computer and use it in GitHub Desktop.
Simple StyleView in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Shared | |
// | |
// Created by Zach Eriksen on 6/24/20. | |
// | |
import SwiftUI | |
import SF | |
class Style: ObservableObject { | |
@Published var padding: CGFloat | |
@Published var margin: CGFloat | |
@Published var borderWidth: CGFloat | |
@Published var cornerRadius: CGFloat | |
@Published var paddingCornerRadius: CGFloat | |
@Published var marginCornerRadius: CGFloat | |
init(padding: CGFloat = 0, | |
margin: CGFloat = 0, | |
borderWidth: CGFloat = 0, | |
cornerRadius: CGFloat = 0, | |
paddingCornerRadius: CGFloat = 0, | |
marginCornerRadius: CGFloat = 0) { | |
self.padding = padding | |
self.margin = margin | |
self.borderWidth = borderWidth | |
self.cornerRadius = cornerRadius | |
self.paddingCornerRadius = paddingCornerRadius | |
self.marginCornerRadius = marginCornerRadius | |
} | |
} | |
struct StyleView: View { | |
@ObservedObject var style: Style | |
var body: some View { | |
Text("Hello World") | |
.padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/, style.padding) | |
.background(Color.red) | |
.cornerRadius(style.paddingCornerRadius) | |
.border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: style.borderWidth) | |
.padding(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/, style.margin) | |
.background(Color.green) | |
.cornerRadius(style.marginCornerRadius) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
StyleView(style: Style()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment