无法更改NSTextField的鼠标光标

hpi*_*que 4 macos cocoa nstextfield nscursor nstrackingarea

我正在尝试更改NSTextField从NIB加载的窗口表中的a 的鼠标光标.

在文档之后,我已经进行了子类化NSTextField和实现resetCursorRects.

- (void) resetCursorRects {
    [self addCursorRect:[self bounds] cursor:[NSCursor pointingHandCursor]];
}
Run Code Online (Sandbox Code Playgroud)

从未调用过.在以下内容中添加以下内容后甚至没有NSWindowViewController:

- (void) windowDidLoad {
    [self.window invalidateCursorRectsForView:self.linkTextField];
}
Run Code Online (Sandbox Code Playgroud)

我还尝试通过在NSTextField子类中添加以下内容来跟踪区域:

- (void) awakeFromNib {
    NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                 options:(NSTrackingCursorUpdate | NSTrackingActiveAlways)
                                                   owner:self
                                                userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void)cursorUpdate:(NSEvent *)theEvent {
    [[NSCursor pointingHandCursor] set];
}
Run Code Online (Sandbox Code Playgroud)

也没用.我究竟做错了什么?

Gir*_*ari 6

可点击链接与NSTextField

NSTextField按照上面提到的子类进行了这个工作:

- (void)resetCursorRects {
    [self addCursorRect:[self bounds] cursor:[NSCursor pointingHandCursor]];
}
Run Code Online (Sandbox Code Playgroud)


Jen*_*sen 1

我遇到了同样的问题,每次光标进入跟踪区域时都会调用cursorUpdate方法,但光标被设置回其他地方,可能是其超级视图。

我设法通过重写 mouseMoved 方法来解决这个问题。

// In the textfield subclass:
- (void)mouseEntered:(NSEvent *)theEvent {
    [super mouseEntered:theEvent];
    self.isMouseIn = YES;
}

- (void)mouseExited:(NSEvent *)theEvent {
    [super mouseExited:theEvent];
    self.isMouseIn = NO;
}


//In the superview of the textfield:
- (void)mouseMoved:(NSEvent *)theEvent {
    if (self.hoverButton.isMouseIn) {
        [[NSCursor pointingHandCursor] set];
    }
    [super mouseMoved:theEvent];
}
Run Code Online (Sandbox Code Playgroud)

我重写了 windowController 类中的 mouseMoved 方法,但重写超级视图应该可以工作。