Skip to content

Instantly share code, notes, and snippets.

@satishVekariya
Last active February 11, 2024 13:48
Show Gist options
  • Save satishVekariya/eef51955fdea90d221bde986c8bdb91e to your computer and use it in GitHub Desktop.
Save satishVekariya/eef51955fdea90d221bde986c8bdb91e to your computer and use it in GitHub Desktop.
SwiftUI draw "U" shape
struct UShape: Shape {
var radius: CGFloat = 8
var inset: CGFloat = 5/2
func path(in rect: CGRect) -> Path {
var path = Path()
let rect = rect.inset(by: .init(top: 0, left: inset, bottom: inset, right: inset))
let width = rect.maxX
let height = rect.maxY
// Drawing the "U" shape
path.move(to: CGPoint(x: width, y: 0))
path.addLine(to: CGPoint(x: width, y: height - radius))
path.addQuadCurve(to: CGPoint(x: width - radius, y: height), control: .init(x: rect.maxX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.minX + radius, y: height))
path.addQuadCurve(to: CGPoint(x: rect.minX, y: height - radius), control: .init(x: rect.minX, y: rect.maxY))
path.addLine(to: rect.origin)
return path
}
}
struct ContentView: View {
var body: some View {
UShape()
.stroke(Color.black, lineWidth: 5)
.frame(width: 200, height: 200)
// .scaleEffect(x: 1, y: -1) // Upside down
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment