簡體   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