【问题标题】:Custom NSButton Focus Ring Color自定义 NSButton 对焦环颜色
【发布时间】:2012-09-30 06:55:56
【问题描述】:

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

问题已更新

【问题讨论】:

  • 对焦环是由cell类绘制的吧?您的子类实现了哪些可能会破坏这一点的方法?
  • 我也继承了 NSButtonCell。

标签: objective-c xcode focus subclass nsbutton


【解决方案1】:

我不知道如何自己绘制对焦环(因此您可以使用自定义颜色),但要让您的自定义按钮单元绘制对焦环,您通常只需像这样覆盖 focusRingMaskBoundsForFrame:inView:

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

如果这不能给你想要的结果,你可能需要通过实现drawFocusRingMaskWithFrame:inView: 来额外提供一个掩码。在这种方法中,您基本上只需绘制应该添加对焦环的内容。 Cocoa 会为您完成剩下的工作。

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

【讨论】:

  • 我根本无法让它工作——那些方法没有被调用,所以我不得不恢复到 10.7 之前的绘制对焦环的方法。网上似乎也没有任何帮助。
【解决方案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
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 2021-10-01
  • 2019-03-08
  • 1970-01-01
相关资源
最近更新 更多