views:

503

answers:

2

Using the plethora of drawing functions in Cocoa or Quartz it's rather easy to draw paths, and fill them using a gradient. I can't seem to find an acceptable way however, to 'stroke'-draw a path with a line width of a few pixels and fill this stroke using a gradient. How is this done?

Edit: Apparently the question wasn't clear enough. Thanks for the responses so far, but I already figured that out. What I want to do is this:

squares

The left square is NSGradient drawn in a path followed by a path stroke message. The right is what I want to do; I want to fill the stroke using the gradient.

+1  A: 

I can't seem to find an acceptable way however, to 'stroke'-draw a path with a line width of a few pixels and fill this stroke using a gradient. How is this done?

[Original answer replaced with the following]

Ah, I see. You want to apply the gradient to the stroke.

To do that, you use a blend mode. I explained how to do this in an answer on another question. Here's the list of steps, adapted to your goal:

  1. Begin a transparency layer.
  2. Stroke the path with any non-transparent color.
  3. Set the blend mode to source in.
  4. Draw the gradient.
  5. End the transparency layer.
Peter Hosey
Thanks for the reply. It's however, not what I try to achieve. I've clarified the question a bit.
Emiel
Emiel: Ah, I see. I've replaced the previous contents of my answer with a new answer.
Peter Hosey
+3  A: 

If you convert the NSBezierPath to a CGPath, you can use the CGContextReplacePathWithStrokedPath() method to retrieve a path that is the outline of the stroked path. Graham Cox's excellent GCDrawKit has a -strokedPath category method on NSBezierPath that will do this for you without needing to drop down to Core Graphics.

Once you have the outlined path, you can fill that path with an NSGradient.

Rob Keniger
Wonderful! Thank you :)Thanks for the link to drawkit too, it looks very nice.
Emiel