NSTextField 事件和辞职第一响应者

发布于 2024-11-27 15:54:27 字数 2555 浏览 0 评论 0原文

我想实现一个 NSTextField,当我单击它时,它会选择所有文本。 (为用户提供删除所有当前文本的简单方法) 当我完成编辑它时,通过按 Enter/Tab 或将鼠标移到其矩形之外,我会将焦点移出该字段,并将其 alpha 值更改为 0.5。

我的代码:

H 文件:

#import <Foundation/Foundation.h>

@interface MoodMsgTextField : NSTextField<NSTextFieldDelegate>

@end

M 文件:

-(BOOL) becomeFirstResponder
{    
    NSLog(@"become first responder");

    BOOL result = [super becomeFirstResponder];
    if(result)
    {
        [self setAlphaValue:1.0];
        [self performSelector:@selector(selectText:) withObject:self afterDelay:0];
    }
    return result;
}

-(BOOL) refusesFirstResponder
{
    return NO;
}

-(BOOL) resignFirstResponder
{
    NSLog(@"resigning first responder");

    BOOL result = [super resignFirstResponder];

    NSText* fieldEditor = [self.window fieldEditor:YES forObject:self];
    [fieldEditor setSelectedRange:NSMakeRange(0,0)];
    [fieldEditor setNeedsDisplay:YES];

    [self setAlphaValue:0.5];

    return  result;
}

-(void)awakeFromNib
{
    self.delegate = self;

    [self setAlphaValue:0.5];    
    [self setBordered:YES];
    [self setWantsLayer:YES];
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [[NSColor grayColor] CGColor];        
}

- (void)controlTextDidChange:(NSNotification *)aNotification
{    
    NSLog(@"the text is %@",self.stringValue);
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
    NSLog(@"end editiing : the text is %@",self.stringValue);
    [self.window makeFirstResponder:nil];
}

- (void)mouseEntered:(NSEvent *)theEvent
{
    [self setWantsLayer:YES];
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [[NSColor grayColor] CGColor];        
}
- (void)mouseExited:(NSEvent *)theEvent
{
    [self setWantsLayer:YES];
    self.layer.borderWidth = 0;
}

所以,我有一些问题:

1.

当我在 NSTextField 内按下(当焦点在外面时)时,它立即成为并退出第一响应者,我开始编辑结束消息。这是为什么 ? 我点击时得到的日志是这样的:

2011-08-02 18:03:19.044 ooVoo[42415:707] become first responder
2011-08-02 18:03:19.045 ooVoo[42415:707] resigning first responder
2011-08-02 18:03:19.104 ooVoo[42415:707] end editing : the text is 

2.

当我按下回车键时,它只会选择其中的所有文本,并且不会移动鼠标焦点。当我按下选项卡时,焦点似乎确实移动了,但是两者都不会导致调用 resignFirstResponder 。为什么 ?

3. 没有调用任何鼠标事件函数。我需要为此做一些特别的事情吗?我想既然它们是 NSResponder 的,我就可以通过继承 NSTextField 来免费获得它们。我这里也需要 NSTrackingInfo 吗?

4.

最后但并非最不重要的一点是,由于某种原因,每一对字母中,有一个字母似乎是粗体的。 我不知道为什么。

我将不胜感激任何帮助。 谢谢

I want to implement an NSTextField where when I click it, it selects all the text. (to give the user easy way to delete all current text)
when I finish editing it, either by pressing enter/tab or moving the mouse outside of it's rect, I will move the focus out of the field, and change it's alpha values to 0.5.

My Code:

H file:

#import <Foundation/Foundation.h>

@interface MoodMsgTextField : NSTextField<NSTextFieldDelegate>

@end

M file:

-(BOOL) becomeFirstResponder
{    
    NSLog(@"become first responder");

    BOOL result = [super becomeFirstResponder];
    if(result)
    {
        [self setAlphaValue:1.0];
        [self performSelector:@selector(selectText:) withObject:self afterDelay:0];
    }
    return result;
}

-(BOOL) refusesFirstResponder
{
    return NO;
}

-(BOOL) resignFirstResponder
{
    NSLog(@"resigning first responder");

    BOOL result = [super resignFirstResponder];

    NSText* fieldEditor = [self.window fieldEditor:YES forObject:self];
    [fieldEditor setSelectedRange:NSMakeRange(0,0)];
    [fieldEditor setNeedsDisplay:YES];

    [self setAlphaValue:0.5];

    return  result;
}

-(void)awakeFromNib
{
    self.delegate = self;

    [self setAlphaValue:0.5];    
    [self setBordered:YES];
    [self setWantsLayer:YES];
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [[NSColor grayColor] CGColor];        
}

- (void)controlTextDidChange:(NSNotification *)aNotification
{    
    NSLog(@"the text is %@",self.stringValue);
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
    NSLog(@"end editiing : the text is %@",self.stringValue);
    [self.window makeFirstResponder:nil];
}

- (void)mouseEntered:(NSEvent *)theEvent
{
    [self setWantsLayer:YES];
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [[NSColor grayColor] CGColor];        
}
- (void)mouseExited:(NSEvent *)theEvent
{
    [self setWantsLayer:YES];
    self.layer.borderWidth = 0;
}

So, I have a few problem:

1.

When I press inside the NSTextField (when the focus is outside) it instantly becomes and resigns first responder and I get editing end message. Why is that ?
The log I get on click is this :

2011-08-02 18:03:19.044 ooVoo[42415:707] become first responder
2011-08-02 18:03:19.045 ooVoo[42415:707] resigning first responder
2011-08-02 18:03:19.104 ooVoo[42415:707] end editing : the text is 

2.

When I press the enter key it just selects all text inside and doesn't move the mouse focus. When I press the tab is does seem to move focus, however neither of the two causes the resignFirstResponder to get called. Why ?

3.
None of the mouse event function are getting called. Do I need to to do something special for that ? I thought that since they are NSResponder's ones, I will get those for free by inheriting from NSTextField. Do I need NSTrackingInfo here as well ?

4.

Last but not least, for some reason, every couple letters, one letter seems to be bold.
I have no idea why.

I'd appreciate any help.
Thanks

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

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

发布评论

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

评论(1

澜川若宁 2024-12-04 15:54:27
  1. 我不确定为什么会发生这种情况,但您应该阅读字段编辑器 概念。基本上 NSTextField 不处理自己的输入,而是使用称为字段编辑器的 NSTextView 来接受输入。

  2. 您需要自己对 Enter 键做出反应。查看密钥处理文档< /a>.这是带有示例的答案

  3. 要获取鼠标事件,您可以使用 NSTrackingArea。请参阅鼠标跟踪的文档.

  4. 我对此没有任何意见,除了有时文本绘制可能看起来很粗体,而实际上发生的是多次绘制文本而不擦除背景。

  1. I am not sure why this is happening in this case but you should read about the Field Editor concept. Basically a NSTextField does not handle its own input but uses an NSTextView called the field editor to accept input.

  2. You need to react to the Enter key yourself. Take a look at the key handling documentation. Here is an answer with an example.

  3. To get mouse events you can use NSTrackingArea. See the docs for Mouse Tracking.

  4. I do not have any input on this except that sometimes text drawing can look bold when really what is happening is that the text is being drawn multiple times without erasing the background.

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