views:

3554

answers:

3

Can anyone guide me in the correct way to build a colored bubble/circle programmatically?

I can't use images as I need it to be able to be any color depending on user interaction.

My thought was maybe to make a white circle image and then overlay a color on top of it. However I am not sure if this would work, or how to really go about it.

If someone could point me the right direction I would appreciate it.

+5  A: 

Create an NSView subclass that holds an NSColor as an ivar. In the drawRect method, create an NSBezierPath of the appropriate size, using the view's bounds. Then set the color [myColor set] and fill the path [myPath fill]. There's a lot more you can do, such as set transparency, a border, and so on and so on, but I'll leave that to the docs unless you have a specific question.

To use the NSView subclass, just drag a view object onto your nib, and choose the name of your subclass in custom class in IB's inspector. You'll need to also set an outlet to it in your controller, so you can change the color as needed.

Marc Charbonneau
Looks like there is even a: bezierPathWithOvalInRect: Thanks!
kdbdallas
Yup, that's new in Leopard if I remember correctly. Before that, one of the very first things newbie Cocoa programmers (including myself) had to do was define a category in NSBezierPath to create a circular or rounded rectangle path. :)
Marc Charbonneau
+1  A: 

Download sketch from apple. http://developer.apple.com/samplecode/Sketch-112/index.html

It can do a lot more, but one of the things is draw circles.

Stephan Eggermont
+6  A: 

There are a couple steps to drawing something in Cocoa.

First you need a path that will be used to define the object that you are going to be drawing. Take a look here Drawing Fundamental Shapes for a guide on creating paths in Cocoa. You will be most interested in sending the "appendBezierPathWithOvalInRect" message to an "NSBezierPath" object, this takes a rectangle that bounds the circle you want to draw.

This code will create a 10x10 circle at coordinates 10,10:

NSRect rect = NSMakeRect(10, 10, 10, 10);
NSBezierPath* circlePath = [NSBezierPath bezierPath];
[circlePath appendBezierPathWithOvalInRect: rect];

Once you have your path you want to set the color for the current drawing context. There are two colors, stroke and fill; stroke is the outline of the path and the fill is the interior color. To set a color you send "set" to an "NSColor" object.

This sets the stroke to black and the fill to red:

[[NSColor blackColor] setStroke];
[[NSColor redColor] setFill];

Now that you have your path and you have your colors set just fill the path and then draw it:

[path stroke];
[path fill];

All of this will need to be done in a graphics context like in drawRect of a view perhaps. All of this together with a graphics context would look like this:

- (void)drawRect:(NSRect)rect
{
    // Get the graphics context that we are currently executing under
    NSGraphicsContext* gc = [NSGraphicsContext currentContext];

    // Save the current graphics context settings
    [gc saveGraphicsState];

    // Set the color in the current graphics context for future draw operations
    [[NSColor blackColor] setStroke];
    [[NSColor redColor] setFill];

    // Create our circle path
    NSRect rect = NSMakeRect(10, 10, 10, 10);
    NSBezierPath* circlePath = [NSBezierPath bezierPath];
    [circlePath appendBezierPathWithOvalInRect: rect];

    // Outline and fill the path
    [circlePath stroke];
    [circlePath fill];

    // Restore the context to what it was before we messed with it
    [gc restoreGraphicsState];
}
joshperry