Reputation: 941
I have the following code which is supposed to draw a stroked and filled rectangle but the fill won't show up.
[[UIColor greenColor] setStroke];
[[UIColor brownColor] setFill];
CGContextBeginPath(context);
CGContextMoveToPoint(context, right, bottom);
CGContextAddLineToPoint(context, right,top);
CGContextAddLineToPoint(context,left, top);
CGContextAddLineToPoint(context, left, bottom);
CGContextAddLineToPoint(context, right, bottom);
CGContextStrokePath(context);
CGContextFillPath(context);
The stroke works and I get a nice green rectangle with no fill (or a white fill). This is within a UIView for iOS. Seems very simple and it's driving me nuts!
Upvotes: 2
Views: 3030
Reputation: 4932
The right way to do this is to set the drawing mode to include both a fill and a path.
CGPathDrawingMode mode = kCGPathFillStroke;
CGContextClosePath( context ); //ensure path is closed, not necessary if you know it is
CGContextDrawPath( context, mode );
You can use CGContextFillPath()
to simply do a fill, but it's basically just CGContextDrawPath()
that automatically calls CGContextClosePath()
and uses kCGPathFill
as the CGPathDrawingMode
. You might as well always use CGContextDrawPath()
and put in your own parameters to get the type of fill/stroke that you want. You can also put holes in convex paths by using one of the Even-Odd drawing modes.
Upvotes: 6
Reputation: 1742
Probably you should call CGContextClosePath before you call CGContextFillPath
Upvotes: 0
Reputation: 15706
From the docs on CGContextStrokePath:
Quartz uses the line width and stroke color of the graphics state to paint the path. As a side effect when you call this function, Quartz clears the current path.
Upvotes: 3