有没有一种方法可以将焦点集中在NSBox上,并为该框绘制对焦环?我以为box drawFocusRingMask;可能是类似的东西,但什么也没发生。我只需要一个框有一个焦点环在它周围时,一个按钮已被点击。
提前谢谢。
发布于 2012-04-07 17:20:45
假设您正在部署到10.7或更高版本,您可以创建一个自定义视图类(可能是NSBox的子类),并让它覆盖以下方法:
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)drawFocusRingMask {
NSRectFill([self bounds]);
}
- (NSRect)focusRingMaskBounds {
return [self bounds];
}如果是NSBox的子类化,如果您愿意,可以使用-borderRect代替bounds。
编辑:您可以使用10.7版本之前风格的焦点环图。您可能会这样做:
-(void)drawRect:(NSRect)rect
{
NSResponder* fr = [[self window] firstResponder];
if ([fr isKindOfClass:[NSView class]] && [(NSView*)fr isDescendantOf:self])
{
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:NSInsetRect([self bounds],4,4)] fill];
[NSGraphicsContext restoreGraphicsState];
}
// ... normal drawing, possibly invoking super ...
}视图还必须监视first responder中的更改,并在其自身上调用-setKeyboardFocusRingNeedsDisplayInRect:。此外,在使用NSFocusRingTypeExterior进行设置期间,它可能需要调用自身的-setFocusRingType:,尽管这可能是默认设置,具体取决于超类。
https://stackoverflow.com/questions/10052628
复制相似问题