Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 556
chinabuffet Avatar asked Dec 04 '12 19:12

chinabuffet


2 Answers

- (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...

like image 99
FluffulousChimp Avatar answered Nov 12 '22 01:11

FluffulousChimp


If you're just looking to save line space, you could define your own method to make two calls and place it in a utility class.

void strokeAndFill(CGContextRef c, CGRect rect)
{
    CGContextFillRect(c, rect);
    CGContextStrokeRect(c, rect);
}
like image 43
MJN Avatar answered Nov 12 '22 03:11

MJN