NSCell 自定义突出显示
我正在尝试子类化 NSCell
来绘制自定义背景突出显示。该文档似乎建议重写的 highlight:withFrame:inView:
应该允许我执行此操作,但该方法永远不会被调用。
相反,我重写了 drawInteriorWithFrame:inView:
,它工作得很好 - 我可以在单元格中绘制我想要的内容。然而,问题是我必须自己绘制所有内容,失去了我正在扩展的 NSCell 类型的功能 - 例如 NSTextFieldCell 显示文本的能力:
自定义绘制突出显示的单元格:
但是,我只想重绘背景(突出显示),并保留使用扩展单元格主要功能的能力:
当然,我也可以自己绘制文本,但我希望有一种更简单的方法来做到这一点。
非常感谢任何帮助。
I'm trying to subclass NSCell
to draw a custom background highlight. The documentation seems to suggest that the overriding highlight:withFrame:inView:
should allow me to do this but the method is never called.
Instead, I've overridden drawInteriorWithFrame:inView:
which works fine - I can draw what I want in the cell. However, the issue is that I have to draw everything myself, losing the functionality of the type of NSCell I am extending - for example an NSTextFieldCell's ability to display text:
Custom drawn highlighted cell:
However, I just want to redraw the background (the highlight), and retain the ability to use the main functionality of the extended cell:
I could, of course, just draw the text myself too but I'm hoping there is an easier way of doing this.
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢@Bvarious 的帮助,我已经成功解决了这个问题。我的扩展 NSTextFieldCell 类实现现在包含:
关键是确保为
highlightColorWithFrame:inView:
返回nil
来停止drawInteriorWithFrame:inView:
绘图背景,但仍然调用它来绘制主要内容(在本例中为文本)。Thanks to the help of @Bavarious I've managed to work it out. My extended NSTextFieldCell class implementation now contains:
The key is to make sure you return
nil
forhighlightColorWithFrame:inView:
to stopdrawInteriorWithFrame:inView:
drawing a background and yet still calling it to draw the main content (in this case text).