void drawRoundedRect(CGContextRef context, CGFloat x, CGFloat y, CGFloat width, CGFloat height, CGFloat rx, CGFloat ry, CGFloat strokeWidth, CGColorRef stroke) { if (rx > (width/2)) { rx = width/2; } if (ry > (height/2)) { ry = height/2; } CGContextSetLineWidth(context, strokeWidth); CGContextSetStrokeColorWithColor(context, stroke); // 1. top left corner CGContextMoveToPoint(context, x+rx, y); // 2. line to right corner CGContextAddLineToPoint(context, x + width - rx, y); // 3. top right rounded corner CGContextAddQuadCurveToPoint(context, x + width, y, x + width, y+ry); // 4. line down the right CGContextAddLineToPoint(context, x + width, y + height - ry); // 5. lower right corner CGContextAddQuadCurveToPoint(context, x + width, y + height, x + width - rx, y + height); // 6. lower line to left corner CGContextAddLineToPoint(context, x + rx, y + height); // 7. lower left corner CGContextAddQuadCurveToPoint(context, x, y+height, x, y + height - ry); // 8. line from lower left to upper left CGContextAddLineToPoint(context, x, y+ry); // 9. upper left corner CGContextAddQuadCurveToPoint(context, x, y, x+rx, y); CGContextStrokePath(context); } void drawRoundedRectFill(CGContextRef context, CGFloat x, CGFloat y, CGFloat width, CGFloat height, CGFloat rx, CGFloat ry, CGFloat strokeWidth, CGColorRef stroke, CGColorRef fill) { if (rx > (width/2)) { rx = width/2; } if (ry > (height/2)) { ry = height/2; } CGContextSetLineWidth(context, strokeWidth); CGContextSetStrokeColorWithColor(context, stroke); CGContextSetFillColorWithColor(context, fill); // 1. top left corner CGContextMoveToPoint(context, x+rx, y); // 2. line to right corner CGContextAddLineToPoint(context, x + width - rx, y); // 3. top right rounded corner CGContextAddQuadCurveToPoint(context, x + width, y, x + width, y+ry); // 4. line down the right CGContextAddLineToPoint(context, x + width, y + height - ry); // 5. lower right corner CGContextAddQuadCurveToPoint(context, x + width, y + height, x + width - rx, y + height); // 6. lower line to left corner CGContextAddLineToPoint(context, x + rx, y + height); // 7. lower left corner CGContextAddQuadCurveToPoint(context, x, y+height, x, y + height - ry); // 8. line from lower left to upper left CGContextAddLineToPoint(context, x, y+ry); // 9. upper left corner CGContextAddQuadCurveToPoint(context, x, y, x+rx, y); CGContextDrawPath(context, kCGPathFillStroke); }