1

有没有办法专注于 NSBox 并为该框绘制对焦环?我想 [box drawFocusRingMask]; 可能是类似的东西,但没有任何反应。当单击按钮时,我只需要该框在其周围有一个焦点环。

提前致谢。

4

1 回答 1

1

假设您要部署到 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 ...
}

The view would also have to watch for changes in the first responder and invoke -setKeyboardFocusRingNeedsDisplayInRect: on itself. Plus, it may need to call -setFocusRingType: on itself during setup with NSFocusRingTypeExterior, although that may be the default, depending on the superclass.

于 2012-04-07T09:20:45.020 回答