将光标设置在文本视图上的指针上 [英] Set the cursor to a pointing hand over a text view

查看:98
本文介绍了将光标设置在文本视图上的指针上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以将光标设置为文本视图上的指针,而无需继承NSTextView?

Is there a way to set the cursor to a pointing hand over a text view without subclassing NSTextView?

我了解了很多有关NSTrackingAreas的知识,测试了很多示例,设置了不同的跟踪选项并实现了不同的方法,但光标仍然是I型光束。我已经读到它是AppKit的自动功能,那么如何防止这种情况发生?

I read a lot about NSTrackingAreas, tested a lot of examples, set different tracking options and implemented different methods, but the cursor still remains an I-Beam. I have read that it is an AppKit automatic feature, so how can I prevent this?

谢谢!

推荐答案

我必须继承。经过几个小时的测试,测试了很多方法和选项,终于成功了:

I had to subclass. After a couple of hours to test a lot of methods and options, this finally worked :

@implementation ATTextView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _trackingArea = [[NSTrackingArea alloc]initWithRect:[self bounds] options: (NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) owner:self userInfo:nil];
        [self addTrackingArea:_trackingArea];
    }
    return self;
}

- (void)mouseMoved:(NSEvent *)event
{
    if ([self isEditable]) [[NSCursor IBeamCursor] set];
    else [[NSCursor pointingHandCursor] set];
}

- (void)updateTrackingAreas {
    [super updateTrackingAreas];
    [self removeTrackingArea:_trackingArea];
    _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options: (NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) owner:self userInfo:nil];
    [self addTrackingArea:_trackingArea];
}
@end

只是找到正确的例子(可可为80 %的文档阅读和20%的编码):
https://developer.apple.com/library/mac…0i-CH8-SW1

Just to find the correct example (Cocoa is 80% doc reading and 20% coding): https://developer.apple.com/library/mac … 0i-CH8-SW1

这篇关于将光标设置在文本视图上的指针上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆