自定义NSButton聚焦环颜色

时间:2012-09-30 06:55:57

标签: objective-c xcode focus subclass nsbutton

我已经将NSButton和NSButtonCell子类化,我已经改变了图纸。现在按钮上不再出现对焦环。我正在寻找一种在自定义按钮上绘制带自定义颜色的聚焦环的方法。

问题已更新

2 个答案:

答案 0 :(得分:1)

我不知道如何自己绘制聚焦环(所以你做自定义颜色),但要让你的自定义按钮单元格绘制聚焦环,你通常只需要覆盖focusRingMaskBoundsForFrame:inView:,如下所示:

- (NSRect)focusRingMaskBoundsForFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    return cellFrame;
}

如果这不能提供您想要的结果,您可能需要通过实施drawFocusRingMaskWithFrame:inView:来另外提供掩码。在这种方法中,你基本上只绘制应该添加焦点环的内容。可可为你做剩下的事。

请注意,这些方法仅适用于Mac OS X 10.7 Lion及更高版本,但苹果公司表示现在应该这样做。

答案 1 :(得分:-2)

您可以为自定义按钮类添加跟踪区域,然后您可以自定义按钮焦点。

-(void)awakeFromNib{
int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.frame options:opts owner:self userInfo:nil];
[self addTrackingArea:trackingArea];
}

现在您可以在以下功能中进行自定义。

- (void) mouseEntered:(NSEvent*)event{
        //Add Custom Focus Ring
}

- (void) mouseExited:(NSEvent*)event{
        //Remove Custom Focus Ring
}
相关问题