One more thing, since you are using almost the same set of modifiers on every view, create your own ViewModifier and use that instead. I have made one for you as an example.
struct cardModifier: ViewModifier {
var fontSize, frameWidth, frameHeight: CGFloat
var frameAlignment: Alignment
var backgroundColour: Color
var backgroundColourOpacity: Double
var shadowRadius: CGFloat
var foregroundColour: Color
var foregroundColourOpacity: Double?
var cardText: String
var overlayFontSize: CGFloat
func body(content: Content) -> some View {
content
.font(.system(size: fontSize))
.padding()
.frame(width: frameWidth, height: frameHeight, alignment: frameAlignment)
.background(Rectangle().fill(backgroundColour .opacity(backgroundColourOpacity)) .shadow(radius: shadowRadius))
.cornerRadius(20)
.foregroundColor(foregroundColour.opacity(foregroundColourOpacity ?? 1))
.overlay(Text(cardText).foregroundColor(.black).padding() .font(.system(size: overlayFontSize)), alignment: .bottom)
}
}
extension View {
func cardStyle(fontSize: CGFloat, frameWidth: CGFloat, frameHeight: CGFloat, frameAlignment: Alignment, backgroundColour: Color, backgroundColourOpacity: Double, shadowRadius: CGFloat, foregroundColour: Color, foregroundColourOpacity: Double? = 1, cardText: String, overlayFontSize: CGFloat) -> some View {
modifier(cardModifier(fontSize: fontSize,
frameWidth: frameWidth,
frameHeight: frameHeight, frameAlignment: frameAlignment,
backgroundColour: backgroundColour,
backgroundColourOpacity: backgroundColourOpacity,
shadowRadius: shadowRadius,
foregroundColour: foregroundColour,
cardText: cardText,
overlayFontSize: overlayFontSize))
}
}
I had to make this a separate post as I ran out of characters in the first one.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: