nonopolarity
nonopolarity

Reputation: 151046

On iOS, how to fill a path with an outline? (or both fill and stroke the path)

On iOS, if we do

CGContextMoveToPoint(contextFoo, 0, 0);
CGContextAddLineToPoint(contextFoo, x, y);
CGContextAddLineToPoint(contextFoo, x2, y2);
// ... and many more CGContextAddLineToPoint

then after this, if we do a CGContextStrokePath, we will get an outline, or if we CGContextFillPath, we get the "fill", but we can't do both, because after a stroke or a fill, the "current path" is gone. How can we both fill and stroke a path (such as wanting a yellow fill and orange outline)?

We can move the MoveTo and AddLine calls to a function, and call that function, do a fill, and call the function again, and do a stroke, but there are many x and y that makes passing all of them to the function quite troublesome. What might be some ways to do this?

Upvotes: 0

Views: 307

Answers (1)

blub
blub

Reputation: 1014

CGContextDrawPath(context, kCGPathFillStroke);
// to both Fill and Stroke your context
// or kCGPathFill/kCGPathStroke to only fill/stroke

you can save your Path btw (to reuse it for multiple things of same shape) using:

CGPathBeginPath/MoveToPoint/AddLine/... very similar to CGContext/...

For your example:

[[UIColor yellowColor] setFill];                      
[[UIColor orangeColor] setStroke];

CGContextDrawPath(context, kCGPathFillStroke);

Upvotes: 3

Related Questions