像 iCal 一样圆形 NSTextFieldCell

发布于 2024-09-17 11:26:11 字数 584 浏览 4 评论 0原文

我正在尝试绘制一个 NSTextFieldCell 子类,它看起来像 iCal 中的圆形事件项普通表。

基于这个问题,我的子类中有以下代码:

- (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];
}

但这只是将单元格绘制为普通矩形,使用渐变填充,但没有圆角。我显然错过了一些东西,但是什么呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情旳空白 2024-09-24 11:26:11

打电话怎么样:

[[textfield cell] setBezelStyle: NSTextFieldRoundedBezel];

What about calling:

[[textfield cell] setBezelStyle: NSTextFieldRoundedBezel];
自由如风 2024-09-24 11:26:11

基于这个问题,我的子类中有以下代码:...

该问题的可接受答案假设单元格位于文本字段中(即,它是视图中唯一的单元格,并且实际上是整个视图),并且该视图是或可以是图层支持的。

当您是表格列的单元格时,这将不起作用,因为您不应该重绘整个视图,并且使其支持图层可能无法正常工作。 (我也不确定人们是否可以期望图层支持的文本字段能够正常工作。除了普通 NSView 之外的任何内容要么可以在图层支持下工作,要么不能;如果文档没有说明是这样,请假设事实并非如此。)

[渐变drawInRect:cellFrame角度:90];

但这只是将单元格绘制为普通矩形,并使用渐变填充,但没有圆角。

是的。这就是该方法所做的全部工作,因此在尚未指定圆角(例如,作为图层的角半径)的情况下,您需要自己构建并绘制带有圆角的形状。

为此, 为带圆角的矩形创建路径,以及在其中绘制渐变

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文