Skip to content

Instantly share code, notes, and snippets.

@nielsbot
Created August 26, 2012 23:47
Show Gist options
  • Save nielsbot/3484435 to your computer and use it in GitHub Desktop.
Save nielsbot/3484435 to your computer and use it in GitHub Desktop.
Function to create a round rect CGPath
CGPathRef CGPathCreateRoundRect( const CGRect r, const CGFloat cornerRadius )
{
CGMutablePathRef p = CGPathCreateMutable() ;
CGPathMoveToPoint( p, NULL, r.origin.x + cornerRadius, r.origin.y ) ;
CGFloat maxX = CGRectGetMaxX( r ) ;
CGFloat maxY = CGRectGetMaxY( r ) ;
CGPathAddArcToPoint( p, NULL, maxX, r.origin.y, maxX, r.origin.y + cornerRadius, cornerRadius ) ;
CGPathAddArcToPoint( p, NULL, maxX, maxY, maxX - cornerRadius, maxY, cornerRadius ) ;
CGPathAddArcToPoint( p, NULL, r.origin.x, maxY, r.origin.x, maxY - cornerRadius, cornerRadius ) ;
CGPathAddArcToPoint( p, NULL, r.origin.x, r.origin.y, r.origin.x + cornerRadius, r.origin.y, cornerRadius ) ;
return p ;
}
@filletofish
Copy link

filletofish commented Mar 5, 2018

Swift code:

 let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 2, height: 8)).cgPath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment