Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate NSTextView?

I tried using defaultParagraphStyle, but I can't seem to get a NSTextView (programmatically added) to truncate text with an ellipsis (...).

I am setting the frame during the initWithFrame (150, 20) and I also set the maxSize (150,20). However, text simply overflows. If I set the height where half a line is visible, I can see the top half of the text, however I would like to see the text truncated with ellipsis instead.

How can I do this?

like image 876
Andrew M Avatar asked Nov 13 '10 07:11

Andrew M


2 Answers

If you hover over the corresponding control in IB, you can see the declaration and brief documentation on the method that it triggers (usually the getter rather than the setter, but it's easy enough to infer one from the other). So for example for the "Line Breaks" popup, you'll see the lineBreakMode method. Then you can use your favorite method to find it in Xcode (e.g. "Open Quickly").

So what you want is:

[[someTextField cell] setLineBreakMode:NSLineBreakByTruncatingTail];

or potentially:

[[someTextField cell] setLineBreakMode:NSLineBreakByCharWrapping];
[[someTextField cell] setTruncatesLastVisibleLine:YES];
like image 194
Nicholas Riley Avatar answered Nov 15 '22 06:11

Nicholas Riley


It is correct what Nicholas wrote.

I like to add (because I spend some time searching for it) if you use Auto Layout for your NSTextView and surrounding NSView you should set your Content Compression Resistance Priority of the NSTextView less than NSLayoutPriorityWindowSizeStayPut (e.g. 499). Otherwise your NSTextView will not truncate its content.

like image 26
Stephan Avatar answered Nov 15 '22 06:11

Stephan