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 NSScrollView
which 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 whole NSTableView
and 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.
If you're using a standard NSTextFieldCell
, just select "Truncates" for its layout value in IB instead of "Wraps".
If you have a custom NSCell
where you're doing your own drawing (I assume this is the case here), you should create an NSParagraphStyle
, set its line break mode, add it as a value for the NSParagraphStyleAttributeName
key in the NSAttributedString
's text attributes dictionary.
An example:
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedStr
addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0,[attributedStr length])];
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:
Cell expansion can occur when the mouse hovers over the specified cell and the cell contents are unable to be fully displayed within the cell.
If they're not working for some reason and you're using a custom NSCell
subclass, make sure you implement -drawWithExpansionFrame:inView:
and -expansionFrameWithFrame:inView:
in your cell. Also make sure you're not returning NO
in your NSTableViewDelegate
for -tableView:shouldShowCellExpansionForTableColumn:row:
(if you have one).
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 -cellSize
for 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With