views:

39

answers:

2

I'm trying to draw an NSTextFieldCell subclass that looks like the rounded event item normal table in iCal.

Based on this question, I've got the following code in my subclass:

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[NSColor lightGrayColor] endingColor:[NSColor grayColor]];
    [gradient drawInRect:cellFrame angle:90];
    controlView.layer.cornerRadius = 0.5f;
    [[self title] drawInRect:cellFrame withAttributes:nil];
}

But this just draws the cell as a normal rectangle, with the gradient fill, but without the rounded corners. I'm obviously missing something, but what?

A: 

What about calling:

[[textfield cell] setBezelStyle: NSTextFieldRoundedBezel];
Lothar
That still draws it as a rectangle with the gradient fill.
Stuart Tevendale
A: 
[gradient drawInRect:cellFrame angle:90];

But this just draws the cell as a normal rectangle, with the gradient fill, but without the rounded corners.

That's what you told it to do.

If you want to draw the gradient in a rectangle with rounded corners, then create a path for a rectangle with rounded corners, and draw the gradient in that.

Peter Hosey
Excellent, Thanks for your help! Just what I wanted.
Stuart Tevendale
In case it helps anyone else, the final code for my class is here: http://www.yellowfield.co.uk/blog/?p=17
Stuart Tevendale