🌅Outlined
可以幫一個 Shape 填色 (fill) 又描邊 (stroke)。

import SwiftUI
// 🌅 Outlined
// - Outlined(shape)
// - Outlined(shape, stroke: stroke)
public struct Outlined<S: Shape, FillContent: ShapeStyle, StrokeContent: ShapeStyle>: View {
// vars
let shape : S
let fillContent : FillContent
let fillStyle : FillStyle
let strokeContent: StrokeContent
let strokeStyle : StrokeStyle
// init
public init(
_ shape: S,
fill : FillContent,
fillStyle : FillStyle = .init(),
stroke : StrokeContent,
strokeStyle: StrokeStyle = .init(lineWidth: 1)
) {
self.shape = shape
self.fillContent = fill
self.fillStyle = fillStyle
self.strokeContent = stroke
self.strokeStyle = strokeStyle
}
// body
public var body: some View {
shape
.fill(fillContent, style: fillStyle)
.overlay(shape.stroke(strokeContent, style: strokeStyle))
}
}
// generic type with specialized init
// - Outlined(shape, fill: .pink, stroke: .purple)
extension Outlined where FillContent == Color, StrokeContent == Color {
public init(
_ shape: S,
fill : FillContent = Color.pink,
fillStyle : FillStyle = .init(),
stroke : StrokeContent = Color.black,
strokeStyle: StrokeStyle = .init(lineWidth: 1)
) {
self.shape = shape
self.fillContent = fill
self.fillStyle = fillStyle
self.strokeContent = stroke
self.strokeStyle = strokeStyle
}
}
Last updated
Was this helpful?