禁用 NSTextField 中的 Option-Enter 换行符?

发布于 2024-07-14 10:37:58 字数 399 浏览 5 评论 0原文

我想禁止在 NSTextField 中输入换行符,默认情况下可以通过键入 Option-Enter(或 Option-Return)来实现。

虽然我发现一些 页面解释如何允许换行而不必须使用选项修饰符,我找不到任何可以完全禁用换行符的东西。

如何才能做到这一点?

I want to disallow entering a new-line character in my NSTextField, which by default is possible by typing Option-Enter (or Option-Return).

While I found some pages explaining how to allow line breaks without having to use the Option modifier, I couldn't find anything for disabling line breaks altogether.

How can this be done?

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

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

发布评论

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

评论(3

山人契 2024-07-21 10:37:58

您可以创建 NSFormatter 的子类并实现 isPartialStringValid:... 方法来阻止换行符。

- (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr 
              originalString:(NSString *)origString 
       originalSelectedRange:(NSRange)origSelRange 
            errorDescription:(NSString **)error
{
    // the user may have:
    // -- deleted the left most character
    // -- deleted the last character and we have an empty string
    // both cases are valid
    if (proposedSelRangePtr->location == 0)
        return YES;

    unichar theChar = [*partialStringPtr characterAtIndex:proposedSelRangePtr->location - 1];

    if ([[NSCharacterSet newlineCharacterSet] characterIsMember:theChar]) {
        *error = nil;
        NSBeep();
        return NO;
    }

    return YES;
}

You could create a subclass of NSFormatter and implement the isPartialStringValid:... method to block the newline.

- (BOOL)isPartialStringValid:(NSString **)partialStringPtr 
       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr 
              originalString:(NSString *)origString 
       originalSelectedRange:(NSRange)origSelRange 
            errorDescription:(NSString **)error
{
    // the user may have:
    // -- deleted the left most character
    // -- deleted the last character and we have an empty string
    // both cases are valid
    if (proposedSelRangePtr->location == 0)
        return YES;

    unichar theChar = [*partialStringPtr characterAtIndex:proposedSelRangePtr->location - 1];

    if ([[NSCharacterSet newlineCharacterSet] characterIsMember:theChar]) {
        *error = nil;
        NSBeep();
        return NO;
    }

    return YES;
}
苏佲洛 2024-07-21 10:37:58

我执行以下代码来防止使用 Control、Option + Enter(回车)时出现换行问题。

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if ((commandSelector == @selector(insertNewlineIgnoringFieldEditor:)) || (commandSelector == @selector(insertLineBreak:)))
    {
        return YES;
    }

    return NO;
}

I do the following code for preventing from line-break issue in case of using Control, Option + Enter (Return).

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if ((commandSelector == @selector(insertNewlineIgnoringFieldEditor:)) || (commandSelector == @selector(insertLineBreak:)))
    {
        return YES;
    }

    return NO;
}
紫罗兰の梦幻 2024-07-21 10:37:58

请问您为什么要禁用此功能? 这是操作系统的一个非常标准的部分。 请记住,用户很可能从其他地方粘贴一些包含换行符的字符串,然后他们将根据需要进行编辑。 保留此功能不变也许会更好,但当需要将值从视图复制到模型时,使用 NSFormatter 删除换行符后的所有内容?

May I ask why you wish to disable this feature? It's a pretty standard part of the OS. Bear in mind that the user might well paste in some string from elsewhere that contains a line break and they'll then edit to their needs. Would it perhaps be better to leave this feature intact, but use an NSFormatter to strip out everything after the line break when it is time to copy the value out of the view to the model?

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