以编程方式制作多行标签

Pru*_*goe 7 cocoa nstextfield

我需要以编程方式制作一些标签和文本字段.我可以到达那里.

    //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];
Run Code Online (Sandbox Code Playgroud)

但我在文档中找不到任何可以让我设置wrap属性的东西.在IB中,属性是布局,其中包含'scroll,wrap和truncate'作为选项.NSTextField没有设置它的方法,如果我上传继承链也没有NSControl.NSView有一个setNeedsLayout方法,但似乎没有相关性:

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.
Run Code Online (Sandbox Code Playgroud)

NSTextFieldCell也没有任何方法.任何帮助,将不胜感激.

小智 9

在OS X 10.11和Swift 2.1中,以下内容帮助了我:

multilineLabel.lineBreakMode = .ByWordWrapping
multilineLabel.setContentCompressionResistancePriority(250, forOrientation: .Horizontal)
Run Code Online (Sandbox Code Playgroud)

如果使用AutoLayout,则需要设置抗压性.


eon*_*ist 5

这对我有用:

textField.usesSingleLineMode = false
textField.cell?.wraps = true
textField.cell?.scrollable = false
Run Code Online (Sandbox Code Playgroud)


Mon*_*olo 1

未经测试,但setWraps:of NSTextFieldCell(继承自NSCell) 应该做你所追求的事情。

要获取单元格,请将cell方法发送到文本字段。NSTextField继承cell了 的方法NSControl,所以如果你想阅读文档,你需要在文档中搜索NSControl.

有关细胞如何工作的更多信息,请参阅NSCell 文档