繁体   English   中英

Cocoa:如何制作多行NSTextField?

[英]Cocoa : How to make multiline NSTextField?

如何制作多行NSTextField? 更新:我在IB特殊类型的NSTextField中找到了“Wrapped Text Field”。 它是多行的但是当我想要换行时我必须按Ctrl + Enter。 但我想只按Enter键获取换行符。 我该怎么做?

无法仅在Interface Builder中指定此行为。 您可以使用委托消息执行此操作,如本技术说明QA1454中所述

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

- (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;
}

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

[[yourTextView textStorage] setAttributedString:attrStr];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM