问题描述
我试图子类化 NSCell
来绘制自定义背景高亮。文档似乎暗示,重写的 highlight:withFrame:inView:
应该允许我这样做,但是该方法从未被调用。
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.
相反,我覆盖了 drawInteriorWithFrame:inView:
这很好 - 我可以在单元格中绘制我想要的。但是,问题是,我必须自己绘制一切,失去NSCell类型的功能,我扩展 - 例如NSTextFieldCell的显示文本的能力:
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:
自定义
但是,我只想重绘背景(突出显示),并保留使用扩展单元格的主要功能的能力:
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.
任何帮助是非常感谢。 / p>
Any help is much appreciated.
推荐答案
感谢@Bavarious的帮助,我已经努力工作了。我的扩展NSTextFieldCell类实现现在包含:
Thanks to the help of @Bavarious I've managed to work it out. My extended NSTextFieldCell class implementation now contains:
-(NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
return nil;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
if ([self isHighlighted]) {
// Draw highlight background here
}
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
关键是要确保你返回 nil
for highlightColorWithFrame:inView:
停止 drawInteriorWithFrame:inView:
绘制背景,它绘制主要内容(在这种情况下是文本)。
The key is to make sure you return nil
for highlightColorWithFrame:inView:
to stop drawInteriorWithFrame:inView:
drawing a background and yet still calling it to draw the main content (in this case text).
这篇关于NSCell自定义亮点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!