自定义 NSButton 和 NSButtonCell
我正在尝试创建自定义按钮外观。
根据我收集的信息,NSButtonCell
负责绘图,所以我实际上应该覆盖它。
但问题是,我的 CustomButton 类还有其他内容,例如 NSImage
、mIsMouseOver
等。目前绘图是在 CustomButton 类中完成的,但我想将其移至细胞。
问题是,我是否可以从 customButtonCell 类访问 customButton 类中的图像,以便我可以使用 [image drawInRect:...]
?
问候, 汉族
I'm trying to create a custom button look.
From what I've gathered, NSButtonCell
does the drawing, so I should actually be overwriting that instead.
But the issue is, my CustomButton class has other things like NSImage
, mIsMouseOver
etc. Currently the drawing is being done in the CustomButton class but I want to move it over to the cell.
question is, is there anyway I can access the image in the customButton class from the customButtonCell class so that I may use [image drawInRect:...]
?
Regards,
Han
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的单元格的
drawWithFrame:(NSRect)frame inView:(NSView *)controlView
方法包含对正在绘制的 NSView 的引用,您可以从中访问视图的属性(例如图像)。Your cell's
drawWithFrame:(NSRect)frame inView:(NSView *)controlView
method includes a reference to the NSView being drawn from which you can access the view's properties (such as image).通常的方法是将数据存储在 NSCell 子类中。基本单元类甚至具有 -(id)image 属性,因此,您的按钮类在查询图像时应调用
[[self cell] image]
。实际上,由于您是 NSButton 的子类,因此它包含您需要的所有内容,只需覆盖单元格的绘图方法即可。如果您需要额外的属性 - 在单元格中定义它,然后将其包装在控件中。
Usual way is to store the data in the NSCell subclass. Base cell class even has an -(id)image property, so, your button class should call
[[self cell] image]
when it is queried for image.Actually, since you are subclassing NSButton, it contains all you need, just override cell's drawing methods. And if you need an extra property - define it in the cell, wrap in the control.