Programmatically make a multi-line shortcut

I need to programmatically make some labels and text fields. I can get there.

    //create the file name label
    NSTextField* newFileNameLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(objXPos, objYPos, 300.0, 20.0)];
    //set properties
    [newFileNameLabel setBordered: NO];
    [newFileNameLabel setTextColor: [NSColor whiteColor]];
    [newFileNameLabel setDrawsBackground:NO];
    [newFileNameLabel setEditable:NO];
    [newFileNameLabel setStringValue: @"File Name:"];

    [vSheetView addSubview:newFileNameLabel];

But I could not find anything in the documents that would allow me to set the wrap property. In IB, a property is a layout with "scroll, wrap, and truncate" options. NSTextField has no way to set this, and if I go to the inheritance chain, neither NSControl. NSView has a setNeedsLayout method, but it does not seem to be related:

You only ever need to invoke this method if your view implements custom layout 
not expressible in the constraint-based layout system by overriding the layout method. 
The system invokes this method automatically for all views using constraints for layout.

NSTextFieldCell also has no methods. Any help would be greatly appreciated.

+5
source share
3 answers

In OS X 10.11 and Swift 2.1, the following helped me:

multilineLabel.lineBreakMode = .ByWordWrapping
multilineLabel.setContentCompressionResistancePriority(250, forOrientation: .Horizontal)

Setting compression resistance is required if you are using AutoLayout.

+6
source

:

textField.usesSingleLineMode = false
textField.cell?.wraps = true
textField.cell?.scrollable = false

: http://stylekit.org/blog/2016/02/07/Text-formating/

+4

Not tested, but setWraps:of NSTextFieldCell(inherited from NSCell) should do what you need.

To get the cell, send the method cellto the text box. NSTextFieldinherits the method cellfrom NSControl, so if you want to read documents, you need to find documents for NSControl.

Learn more about how cells work in NSCell Docs .

+1
source

All Articles