gpt4 book ai didi

objective-c - NSBox 对焦环?

转载 作者:行者123 更新时间:2023-12-03 17:14:46 25 4
gpt4 key购买 nike

有没有办法聚焦在 NSBox 上并为该框绘制聚焦环?我想 [box drawFocusRingMask];可能是类似的事情,但什么也没有发生。我只需要当单击按钮时盒子周围有一个聚焦环。

提前致谢。

最佳答案

假设您要部署到 10.7 或更高版本,您可以创建一个自定义 View 类(可能是 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 ...
}

View 还必须监视第一响应者中的变化,并对其自身调用 -setKeyboardFocusRingNeedsDisplayInRect: 。另外,在使用 NSFocusRingTypeExterior 设置期间,它可能需要自行调用 -setFocusRingType:,尽管这可能是默认设置,具体取决于父类(super class)。

关于objective-c - NSBox 对焦环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10052628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com