1

How do I render multi-line centered text, limited to a specific frame size, along with inserting the … (ellipsis) char when the text doesn't fit?

Background:

I am developing a custom view that mimicks OS X Finder's "as Icons" view, with the Label position set to "Bottom". In this view mode, file names appear below icons in two rows, like this (with ASCII art for the bounding frame):

+------------------------+
|  long file name that   |
| gets centered…rest.jpg |
+------------------------+

I understand that an NSTextField can almost accomplish that, but not completely: With the settings "Wrap" and "Truncate Last Visible Line" (as offered in Xcode's IB), it ends up like this:

+------------------------+
|  long file name that   |
| gets centered and mor… |
+------------------------+

I.e, it truncates the text at the end and not in the middle of the last visible line.

Also, I cannot use a NSControl but need to render the text into a graphics context instead.

Is there any framework function that can do this, or is this code private to the Finder and I'll need to rewrite all that myself?

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
  • 1
    Possible duplicate of http://stackoverflow.com/questions/11138786/truncate-the-last-line-of-multi-line-nstextfield . The accepted answer there talks about `NSTextField`, but I doubt that you'd really need a control. Note that `NSLineBreakByCharWrapping` can be used in a paragraph style in an attributed string. – JWWalker Apr 24 '15 at 22:16
  • Well spotted, thanks! I recognize your name, too. Alias Dragon FTW! :) – Thomas Tempelmann Apr 25 '15 at 00:22
  • You have a good memory, I can barely remember Alias Dragon. – JWWalker Apr 25 '15 at 01:19
  • The linked code had issued which I have fixed now. I guess this question can be closed as a dupe, then. – Thomas Tempelmann Apr 25 '15 at 17:28

0 Answers0