Created
August 30, 2022 19:24
-
-
Save RickyAvina/05e93443c57f98b5f1663cf00f49fe0c to your computer and use it in GitHub Desktop.
Add a border to a specific set of edges of a view
This file contains 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
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