如何判断 NSTextFieldCell isHighlighted 何时没有焦点?

发布于 2024-11-27 03:44:34 字数 490 浏览 0 评论 0原文

我已经对 NSTextFieldCell (在 NSTableView 内)进行了子类化,以在选择单元格(即行)时绘制自定义前景色(例如 isHighlighted 为 true)并且一切正常。

问题是当表视图失去焦点时我想用不同的颜色绘制选定的行,如何确定包含单元格的表视图是否不是 drawWithFrame:(NSRect)cellFrame inView:(NSView 内的第一响应者*)控制视图?

我当前的代码是

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
        NSColor* textColor = [self isHighlighted]
                 ? [NSColor alternateSelectedControlTextColor]
                 : [NSColor darkGrayColor];
}

I've subclassed a NSTextFieldCell (inside a NSTableView) to draw a custom foreground color when a cell (ie row) is selected (eg isHighlighted is true) and everything works fine.

The problem is when the table view loses the focus I want to draw the selected rows with a different color, how can I determine if the table view containing the cell isn't the first responder inside drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView?

My current code is

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
        NSColor* textColor = [self isHighlighted]
                 ? [NSColor alternateSelectedControlTextColor]
                 : [NSColor darkGrayColor];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2024-12-04 03:44:36

我找到了一个使用firstResponder的解决方案,它很简单而且看起来很高效

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
        NSColor* textColor;

        if ([self isHighlighted]) {
            textColor = [[controlView window] firstResponder] == controlView 
                     ? [NSColor alternateSelectedControlTextColor]
                     : [NSColor yellowColor];
        } else {
            textColor = [NSColor darkGrayColor];
        }

        // use textColor
        ...
        ...
        [super drawWithFrame:cellFrame inView:controlView];
    }

I've found a solution that uses the firstResponder, it is simple and seems efficient

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
        NSColor* textColor;

        if ([self isHighlighted]) {
            textColor = [[controlView window] firstResponder] == controlView 
                     ? [NSColor alternateSelectedControlTextColor]
                     : [NSColor yellowColor];
        } else {
            textColor = [NSColor darkGrayColor];
        }

        // use textColor
        ...
        ...
        [super drawWithFrame:cellFrame inView:controlView];
    }
寂寞美少年 2024-12-04 03:44:36

还有一件事,上面的代码是完美的,但是如果你有多个窗口
你需要检查你的窗户是否是关键

        if (controlView && ([[controlView window] firstResponder] == controlView) && [[controlView window] isKeyWindow]) {
            [attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
        }

one more thing, the above code is perfect, however if you have multiple windows
you will need to check if your window is key

        if (controlView && ([[controlView window] firstResponder] == controlView) && [[controlView window] isKeyWindow]) {
            [attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
        }
所谓喜欢 2024-12-04 03:44:35

我发现最好的方法不会让您处理响应者(因为有时 controlView 的超级视图是响应者或一些废话)是使用编辑器:

BOOL isEditing = [(NSTextField *)[self controlView] currentEditor] != nil;

就这么简单!

The best way I've found that doesn't make you deal with responders (since sometimes the controlView's superview is the responder or some nonsense) is to use the editor:

BOOL isEditing = [(NSTextField *)[self controlView] currentEditor] != nil;

Easy as that!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文