Cocoa:如何制作多行 NSTextField?

发布于 2024-10-31 19:59:23 字数 155 浏览 1 评论 0原文

如何制作多行 NSTextField? 更新:我在IB中发现了一种特殊类型的NSTextField,称为“Wrapped Text Field”。它是多行的,但是当我想要换行时,我必须按 Ctrl+Enter。但我只想按 Enter 以获得换行符。我该怎么做呢?

How to make multiline NSTextField? UPDATE: I've found in IB special type of NSTextField called "Wrapped Text Field". It is multiline but when I want get a newline I have to press Ctrl+Enter. But I want to press only Enter to get a newline. How can I do it?

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

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

发布评论

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

评论(3

合久必婚 2024-11-07 19:59:23

无法仅在 Interface Builder 中指定此行为。您可以按照本技术说明 QA1454< 中所述使用委托消息来完成此操作/a>.

以下是技术说明中的委托消息示例:

- (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;
    }
    else if (commandSelector == @selector(insertTab:))
    {
        // tab action:
        // always insert a tab character and don’t cause the receiver to end editing
        [textView insertTabIgnoringFieldEditor:self];
        result = YES;
    }

    return result;
}

There is no way to specify this behavior solely in Interface Builder. You can do it with a delegate message as described in this tech note QA1454.

Here is the example delegate message from the tech note:

- (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;
    }
    else if (commandSelector == @selector(insertTab:))
    {
        // tab action:
        // always insert a tab character and don’t cause the receiver to end editing
        [textView insertTabIgnoringFieldEditor:self];
        result = YES;
    }

    return result;
}
爱格式化 2024-11-07 19:59:23

使用NSTextView,它是一个多行NSTextField,它是NSText的子类,如果我错了,请纠正我。 NSTextView 有一个 NSTextStorage,它是 NSAttributedString 的子类。您需要给它一个 NSAttributedString 对象而不是 NSString 来填充其内容,因为它可以显示颜色等。

[[yourTextView textStorage] setAttributedString:attrStr];

Using NSTextView, its a multiline NSTextField sorta, it is a subclass of NSText correct my if I am wrong. The NSTextView has an NSTextStorage, which is a subclass of NSAttributedString. You need to give it an NSAttributedString object instead of a NSString to fill its contents as it can display colors etc.

[[yourTextView textStorage] setAttributedString:attrStr];
吾性傲以野 2024-11-07 19:59:23

与 Jon Steinmetz 的答案相同,但对于 Swift:

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
        if commandSelector == #selector(NSControl.insertNewline)
        {
            // new line action:
            // always insert a line-break character and don’t cause the receiver to end editing
            textView.insertNewlineIgnoringFieldEditor(self)
            return true
        }
        else if commandSelector == #selector(NSControl.insertTab)
        {
            // tab action:
            // always insert a tab character and don’t cause the receiver to end editing
            textView.insertTabIgnoringFieldEditor(self)
            return true
        }

        return false
    }

我设置的委托协议是 NSTextFieldDelegate

The same answer as Jon Steinmetz but for Swift:

func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
        if commandSelector == #selector(NSControl.insertNewline)
        {
            // new line action:
            // always insert a line-break character and don’t cause the receiver to end editing
            textView.insertNewlineIgnoringFieldEditor(self)
            return true
        }
        else if commandSelector == #selector(NSControl.insertTab)
        {
            // tab action:
            // always insert a tab character and don’t cause the receiver to end editing
            textView.insertTabIgnoringFieldEditor(self)
            return true
        }

        return false
    }

The delegate protocol I set up was NSTextFieldDelegate

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