Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa : How to make multiline NSTextField?

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?

like image 788
Siarhei Fedartsou Avatar asked Apr 10 '11 11:04

Siarhei Fedartsou


2 Answers

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;
}
like image 96
Jon Steinmetz Avatar answered Oct 23 '22 05:10

Jon Steinmetz


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];
like image 40
Antwan van Houdt Avatar answered Oct 23 '22 05:10

Antwan van Houdt