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!
The right way to do this is to set the drawing mode to include both a fill and a path.
You can use
CGContextFillPath()to simply do a fill, but it’s basically justCGContextDrawPath()that automatically callsCGContextClosePath()and useskCGPathFillas theCGPathDrawingMode. You might as well always useCGContextDrawPath()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.