I have an NSTableView that has 2 columns, one for an icon and the other for two lines of text. In the second column, the text column, I have some larger text that is for the name of an item. Then I have a new line and some smaller text that describes the state of the item.
When the name becomes so large that it doesn’t fit on one line it wraps (or when you shrink the window down so small that it causes the names to not fit on a single line).
row1===============
| image | some name |
| image | idle |
row2================
| image | some name really long name | <- this gets wrapped pushing ‘idle’ out of the view
| image | idle |
===================
My question is, how could I keep the text from wrapping and just have the NSTableView display a horizontal scroll-bar once the name is too large to fit?
Scrolling in Cocoa is implemented with the
NSScrollViewwhich is a view instead of a cell so if you really want to implement horizontal scrolling for table view cells I think you’d have to subclass the wholeNSTableViewand implement the feature there. My suggestion (without knowing the specifics of your situation, of course) is that you don’t do that, though, since it’s nonstandard behaviour and would probably entail quite a bit of work.Truncate Instead of Wrap
If you’re using a standard
NSTextFieldCell, just select “Truncates” for its layout value in IB instead of “Wraps”.If you have a custom
NSCellwhere you’re doing your own drawing (I assume this is the case here), you should create anNSParagraphStyle, set its line break mode, add it as a value for theNSParagraphStyleAttributeNamekey in theNSAttributedString‘s text attributes dictionary.An example:
Cell Expansion Frames
If you don’t want to wrap your lines of text in the table view cells, the standard method of allowing the user to see the whole text is to use cell expansion frames which are enabled by default:
If they’re not working for some reason and you’re using a custom
NSCellsubclass, make sure you implement-drawWithExpansionFrame:inView:and-expansionFrameWithFrame:inView:in your cell. Also make sure you’re not returningNOin yourNSTableViewDelegatefor-tableView:shouldShowCellExpansionForTableColumn:row:(if you have one).Adjust Width of Whole Table View?
If what you want to do is to adjust the width of a specific column (and thus the whole table view, possibly causing the enclosing scroll view’s horizontal scroll bar to appear) such that the text its cells contain would never be truncated or wrapped, you can probably do that in your
NSTableViewDelegate, for example, by calling-cellSizefor each row’s cell in that column and resizing the column to the largest value (you’ll want to only do this when the values change, of course).