5

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

NSTextField根据文档,我对resetCursorRects.

- (void) resetCursorRects {
    [self addCursorRect:[self bounds] cursor:[NSCursor pointingHandCursor]];
}

这永远不会被调用。即使在添加以下内容后也不行NSWindowViewController

- (void) windowDidLoad {
    [self.window invalidateCursorRectsForView:self.linkTextField];
}

我还尝试通过在子类中添加以下内容来使用跟踪区域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];
}

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

4

5 回答 5

7

带有 NSTextField 的可点击链接

NSTextField如前所述,我在子类化后得到了这个工作:

- (void)resetCursorRects {
    [self addCursorRect:[self bounds] cursor:[NSCursor pointingHandCursor]];
}
于 2013-09-04T11:03:43.293 回答
2

NSTextView 似乎正在通过它的-(void)mouseMoved:(NSEvent*)theEvent方法处理光标。此外,当 NSTextView 成为第一响应者时,RunLoop 中的一些私有代码似乎将光标强制到 IBeamCursor 而不给我们选择。这是一个解决这些限制的子类:

@interface MyTextView : NSTextView {
    NSTrackingArea* area;
    BOOL mouseInside;
}

@property(nonatomic, retain) NSTrackingArea* area;

@end


@implementation MyTextView

@synthesize area;

- (void)setArea:(NSTrackingArea *)newArea
{
    [newArea retain];
    if (area) {
        [self removeTrackingArea:area];
        [area release];
    }
    if (newArea) {
        [self addTrackingArea:newArea];
    }
    area = newArea;
}

- (BOOL)becomeFirstResponder
{
    NSRect rect = <insert the tracking rect where you want to have a special cursor>;
    self.area = [[[NSTrackingArea alloc] initWithRect:rect options:NSTrackingMouseEnteredAndExited | NSTrackingActiveWhenFirstResponder owner:self userInfo:nil] autorelease];
    NSEvent* ev = [NSApp currentEvent];
    if (NSPointInRect([self convertPoint:ev.locationInWindow fromView:nil], self.bounds)) {
        mouseInside = YES;
        // This is a workaround for the private call that resets the IBeamCursor
        [[NSCursor arrowCursor] performSelector:@selector(set) withObject:nil afterDelay:0];
    }
}

- (void)mouseEntered:(NSEvent *)theEvent
{
    [super mouseEntered:theEvent];
    [[NSCursor arrowCursor] set];
    mouseInside = YES;
}

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

- (void)mouseMoved:(NSEvent *)theEvent
{
    si (!mouseInside) {
        // We only forward the mouseMoved event when the mouse is outside the zone for which we control the cursor
        [super mouseMoved:theEvent];
    }
}

- (oneway void)dealloc
{
    [area release];
    [super dealloc];
}

@end
于 2014-02-06T16:03:55.377 回答
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];
}

我在 windowController 类中重写了 mouseMoved 方法,但是重写 superview 应该可以工作。

于 2013-04-06T16:50:06.687 回答
1

我编写了上述答案的 Swift 2.0 版本,并将其作为GitHub 示例项目在此处找到

即使它是为 NSTextField 设置的,这些概念也应该适用于任何子类的来源NSResponder(这是从哪里来的) mouseEnteredbecomeFirstResponder

这是我编写的代码的核心内容:

import Cocoa

class CCTextField: NSTextField {

    var myColorCursor : NSCursor?

    var mouseIn : Bool = false

    var trackingArea : NSTrackingArea?

    override func awakeFromNib()
    {
        myColorCursor = NSCursor.init(image: NSImage(named:"heart")!, hotSpot: NSMakePoint(0.0, 0.0))
    }

    override func resetCursorRects() {
        if let colorCursor = myColorCursor {
            self.addCursorRect(self.bounds, cursor: colorCursor)
        }
    }

    override func mouseEntered(theEvent: NSEvent) {
        super.mouseEntered(theEvent)
        self.mouseIn = true
    }

    override func mouseExited(theEvent: NSEvent) {
        super.mouseExited(theEvent)
        self.mouseIn = false
    }

    override func mouseMoved(theEvent: NSEvent) {
        if self.mouseIn {
            myColorCursor?.set()
        }
        super.mouseMoved(theEvent)
    }

    func setArea(areaToSet: NSTrackingArea?)
    {
        if let formerArea = trackingArea {
            self.removeTrackingArea(formerArea)
        }

        if let newArea = areaToSet {
            self.addTrackingArea(newArea)
        }
        trackingArea = areaToSet
    }

    override func becomeFirstResponder() -> Bool {
        let rect = self.bounds
        let trackingArea = NSTrackingArea.init(rect: rect, options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways], owner: self, userInfo: nil)

        // keep track of where the mouse is within our text field
        self.setArea(trackingArea)

        if let ev = NSApp.currentEvent {
            if NSPointInRect(self.convertPoint(ev.locationInWindow, fromView: nil), self.bounds) {
                self.mouseIn = true
                myColorCursor?.set()
            }
        }
        return true
    }
}
于 2015-11-04T11:34:30.883 回答
0

这对我有用:

class TextField: NSTextField {
    override func becomeFirstResponder() -> Bool {
        addTrackingAreaIfNeeded()
        return super.becomeFirstResponder()
    }
    override func mouseMoved(with event: NSEvent) {
        super.mouseMoved(with: event)
        NSCursor.pointingHand.set()
    }
    private func addTrackingAreaIfNeeded() {
        if trackingAreas.isEmpty {
            let area = NSTrackingArea(rect: bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil)
            addTrackingArea(area)
        }
    }
}
于 2017-11-24T23:24:56.490 回答