In my previous post about round rects in Quartz/CoreGraphics I posted a function I’m constantly using for drawing round rects. Today I found a bug in it. Apparently, all the round rects I drew were placed at 0,0 and so I never stumbled on the bug. Here’s a fixed version of the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | void DPContextAddRoundRect(CGContextRef context, CGRect rect, CGFloat cornerRad) { // Top Left CGContextMoveToPoint(context, rect.origin.x, cornerRad); CGContextAddArcToPoint(context, rect.origin.x, rect.origin.y, rect.origin.x + cornerRad, rect.origin.y, cornerRad); // Top right CGContextAddArcToPoint(context, rect.origin.x + rect.size.width, rect.origin.y, rect.origin.x + rect.size.width, rect.origin.x + cornerRad, cornerRad); // Bottom right CGContextAddArcToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, rect.origin.x + rect.size.width - cornerRad, rect.origin.y + rect.size.height, cornerRad); // Bottom left CGContextAddArcToPoint(context, rect.origin.x, rect.origin.y + rect.size.height, rect.origin.x, rect.origin.y, cornerRad); CGContextClosePath(context); } |

