Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make nstextfield single line

how to make NSTextField really single line?

I created a text field programmatically. when the return key is pressed, all text is selected. but I can still paste multiple lines of text. And when I press Arrow-right or Arrow down, it scroll to the next line.

There aren't these issues if I use IB and set the "use single line mode", but I couldn't find the right method to set it programmatically.

like image 907
Jensen Avatar asked Feb 02 '13 06:02

Jensen


2 Answers

Have you tried telling the field's cell to set whether it uses single-line mode?

[myTextField.cell setUsesSingleLineMode:YES];
like image 91
Peter Hosey Avatar answered Oct 09 '22 16:10

Peter Hosey


This works for me when programmatically creating controls:

[myTextField.cell setWraps:NO];
[myTextField.cell setScrollable:YES];

Keep in mind that direct access to NSControl's cell has been discouraged since OS X 10.10 introduction:

Gradual deprecation of NSCell

Mac OS X 10.10 takes another step towards the eventual deprecation of cells. Direct access to the cell of a control is discouraged, and methods which allow it will be formally deprecated in a subsequent release. A variety of cell-level APIs have been promoted to various Control subclasses in order to provide cell-free access to important functionality. NSLevelIndicator, NSTextField, NSSearchField, NSSlider, and NSPathControl all have new properties for this purpose. Cell-based NSTableViews are now deprecated, and view-based NSTableViews should be used instead. Matrix-based NSBrowsers are also deprecated in favor of the item-based interface.

https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/

like image 20
mzf Avatar answered Oct 09 '22 16:10

mzf