无论如何,要让(包装)NSTextField 在按回车键时写入回车符?

发布于 2024-11-09 05:10:20 字数 93 浏览 3 评论 0原文

我想在我的应用程序中使用可能包含回车符的换行文本字段。有没有办法强制 NSTextField 对象在按下 Return 键时将回车符写入文本区域,而不是将其操作发送到目标?

I want to use a wrapping text field that can potentially contain carriage returns in my app. Is there any way to force the NSTextField object to write a carriage return into the text area instead of sending its action to its target when the Return key is pressed?

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

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

发布评论

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

评论(4

隱形的亼 2024-11-16 05:10:20

技术问答 QA1454 对此进行了介绍,其中还介绍了列举了在这种情况下使用 NSTextField 而不是 NSTextView 的原因。

您可以在文本字段委托中实现以下方法:

- (BOOL)control:(NSControl*)control
    textView:(NSTextView*)textView
    doCommandBySelector:(SEL)commandSelector
{
    BOOL result = NO;

    if (commandSelector == @selector(insertNewline:))
    {
        // new line action:
        // always insert a line-break character and don’t cause the receiver
        // to end editing
        [textView insertNewlineIgnoringFieldEditor:self]; 
        result = YES;
    }

    return result;
}

This is covered in Technical Q&A QA1454, which also enumerates reasons why one would use NSTextField instead of NSTextView in this case.

You can implement the following method in the text field delegate:

- (BOOL)control:(NSControl*)control
    textView:(NSTextView*)textView
    doCommandBySelector:(SEL)commandSelector
{
    BOOL result = NO;

    if (commandSelector == @selector(insertNewline:))
    {
        // new line action:
        // always insert a line-break character and don’t cause the receiver
        // to end editing
        [textView insertNewlineIgnoringFieldEditor:self]; 
        result = YES;
    }

    return result;
}
千と千尋 2024-11-16 05:10:20

好吧,我找到了一种方法,但这可能不是最好的(甚至不是好的)方法。我对 NSTextField 进行子类化,并覆盖 -textShouldEndEditing: ,如下所示:

-(BOOL)textShouldEndEditing:(NSText *)textObject {
    NSEvent * event = [[NSApplication sharedApplication] currentEvent];
    if ([event type] == NSKeyDown && [event keyCode] == 36) {
        [self setStringValue:[[self stringValue] stringByAppendingString:@"\n"]];
        return NO;
    }
    else {
        return [super textShouldEndEditing:textObject];
    }
}

Okay, I figured out one way to do it, but this very well may not be the best (or even a good) way. I subclassed NSTextField, and overrode -textShouldEndEditing: like so:

-(BOOL)textShouldEndEditing:(NSText *)textObject {
    NSEvent * event = [[NSApplication sharedApplication] currentEvent];
    if ([event type] == NSKeyDown && [event keyCode] == 36) {
        [self setStringValue:[[self stringValue] stringByAppendingString:@"\n"]];
        return NO;
    }
    else {
        return [super textShouldEndEditing:textObject];
    }
}
白云悠悠 2024-11-16 05:10:20

我发现 Sean 和 Bevarious 的组合最适合我。肖恩的回答假设新行总是希望添加到末尾(而不是例如用户光标所在的位置)。

-(BOOL)textShouldEndEditing:(NSText *)textObject 
{
    NSEvent * event = [[NSApplication sharedApplication] currentEvent];
    if ([event type] == NSKeyDown && [event keyCode] == 36) 
    {
        [textObject insertNewlineIgnoringFieldEditor:nil];
        return NO;
    }
    else 
    {
        return [super textShouldEndEditing:textObject];
    }
}

I found a combination of Sean and Bevarious worked best for me. Sean's answer assumes that the new line is always wanted to be added to the end (instead of for instance where the user's cursor is placed).

-(BOOL)textShouldEndEditing:(NSText *)textObject 
{
    NSEvent * event = [[NSApplication sharedApplication] currentEvent];
    if ([event type] == NSKeyDown && [event keyCode] == 36) 
    {
        [textObject insertNewlineIgnoringFieldEditor:nil];
        return NO;
    }
    else 
    {
        return [super textShouldEndEditing:textObject];
    }
}
旧城空念 2024-11-16 05:10:20

斯威夫特版本:

override func textShouldEndEditing(textObject: NSText) -> Bool {
    let event = NSApplication.sharedApplication().currentEvent
    if event?.type == NSEventType.KeyDown && event?.keyCode == 36 {
        self.stringValue = self.stringValue.stringByAppendingString("\n")
        return false
    } else {
        return super.textShouldEndEditing(textObject)
    }
}

Swift version:

override func textShouldEndEditing(textObject: NSText) -> Bool {
    let event = NSApplication.sharedApplication().currentEvent
    if event?.type == NSEventType.KeyDown && event?.keyCode == 36 {
        self.stringValue = self.stringValue.stringByAppendingString("\n")
        return false
    } else {
        return super.textShouldEndEditing(textObject)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文