开发者

Rounded NSTextFieldCell like iCal

开发者 https://www.devze.com 2023-01-14 17:47 出处:网络
I\'m trying to draw an NSTextFieldCell subclass that looks like the rounded event item normal table in iCal.

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 obvi开发者_开发技巧ously missing something, but what?


What about calling:

[[textfield cell] setBezelStyle: NSTextFieldRoundedBezel];


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

The accepted answer on that question assumes that the cell is in a text field (i.e., it is the only cell in the view and it effectively is the entire view), and that that view is or can be layer-backed.

That won't work when you're a table column's cell, because you are not supposed to redraw the whole view and making it layer-backed probably isn't going to work correctly. (I'm not sure one can expect layer-backing a text field to work correctly, either. Anything beyond a plain NSView either is made to work layer-backed or isn't; if the documentation doesn't say it is, assume it isn't.)

[gradient drawInRect:cellFrame angle:90];

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

Yup. That's all this method does, so without the rounded corners being already specified (e.g., as the corner radius of a layer), you need to construct and draw the shape with rounded corners yourself.

To do that, create a path for a rectangle with rounded corners, and draw the gradient in that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号