Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you keep text from wrapping in an NSTableView using NSAttributedString

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?

like image 482
Justin Avatar asked May 24 '10 22:05

Justin


1 Answers

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.

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 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])];

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:

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).

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 -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).

like image 181
hasseg Avatar answered Oct 29 '22 05:10

hasseg