Skip to content

Instantly share code, notes, and snippets.

@RickyAvina
Created August 30, 2022 19:24
Show Gist options
  • Save RickyAvina/05e93443c57f98b5f1663cf00f49fe0c to your computer and use it in GitHub Desktop.
Save RickyAvina/05e93443c57f98b5f1663cf00f49fe0c to your computer and use it in GitHub Desktop.
Add a border to a specific set of edges of a view
extension View {
func border(width: CGFloat, edges: [Edge], color: Color) -> some View {
overlay(EdgeBorder(width: width, edges: edges).foregroundColor(color))
}
}
struct EdgeBorder: Shape {
var width: CGFloat
var edges: [Edge]
func path(in rect: CGRect) -> Path {
var path = Path()
for edge in edges {
var x: CGFloat {
switch edge {
case .top, .bottom, .leading: return rect.minX
case .trailing: return rect.maxX - width
}
}
var y: CGFloat {
switch edge {
case .top, .leading, .trailing: return rect.minY
case .bottom: return rect.maxY - width
}
}
var w: CGFloat {
switch edge {
case .top, .bottom: return rect.width
case .leading, .trailing: return self.width
}
}
var h: CGFloat {
switch edge {
case .top, .bottom: return self.width
case .leading, .trailing: return rect.height
}
}
path.addPath(Path(CGRect(x: x, y: y, width: w, height: h)))
}
return path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment