Search code examples
objective-cioscore-graphics

Core graphics draw rectangle with border in one line


How can I draw a rectangle with a border in one line?

There are separate methods like:

CGContextStrokeRect(context, someRectangle);

and

CGContextFillRect(context, someRectangle);

but is there something that does both in one?


Solution

  • - (void)drawRect:(CGRect)rect {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGPathRef path = CGPathCreateWithRect(rect, NULL);
        [[UIColor redColor] setFill];
        [[UIColor greenColor] setStroke];
        CGContextAddPath(context, path);
        CGContextDrawPath(context, kCGPathFillStroke);
        CGPathRelease(path);
    }
    

    Although, I can't say it's any less verbose than stroke & fill in separate calls...