像 iCal 一样圆形 NSTextFieldCell
我正在尝试绘制一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打电话怎么样:
What about calling:
该问题的可接受答案假设单元格位于文本字段中(即,它是视图中唯一的单元格,并且实际上是整个视图),并且该视图是或可以是图层支持的。
当您是表格列的单元格时,这将不起作用,因为您不应该重绘整个视图,并且使其支持图层可能无法正常工作。 (我也不确定人们是否可以期望图层支持的文本字段能够正常工作。除了普通 NSView 之外的任何内容要么可以在图层支持下工作,要么不能;如果文档没有说明是这样,请假设事实并非如此。)
是的。这就是该方法所做的全部工作,因此在尚未指定圆角(例如,作为图层的角半径)的情况下,您需要自己构建并绘制带有圆角的形状。
为此, 为带圆角的矩形创建路径,以及在其中绘制渐变。
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.)
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.