Skip to content

Instantly share code, notes, and snippets.

@mdb1
Last active January 3, 2023 22:45
Show Gist options
  • Select an option

  • Save mdb1/3e7df86735db7d3417c57878e286e77a to your computer and use it in GitHub Desktop.

Select an option

Save mdb1/3e7df86735db7d3417c57878e286e77a to your computer and use it in GitHub Desktop.
RoundedOverlayBorder modifier
/// Extension to apply a RoundedOverlayBorder modifier.
extension View {
/// Adds a rounded overlay border to the view.
func roundedOverlayBorder(
cornerRadius: CGFloat,
color: Color = .gray,
lineWidth: CGFloat = 1
) -> some View {
modifier(
RoundedOverlayBorderModifier(
cornerRadius: cornerRadius,
color: color,
lineWidth: lineWidth
)
)
}
}
/// A modifier that adds a `rounded rectangle` overlay border to the view.
struct RoundedOverlayBorderModifier: ViewModifier {
private var cornerRadius: CGFloat
private var color: Color
private var lineWidth: CGFloat
init(
cornerRadius: CGFloat,
color: Color,
lineWidth: CGFloat = 1
) {
self.cornerRadius = cornerRadius
self.color = color
self.lineWidth = lineWidth
}
func body(content: Content) -> some View {
content.overlay(
RoundedRectangle(cornerRadius: cornerRadius)
.stroke(color, lineWidth: lineWidth)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment