Modeling UILabel behavior on OSX

I want to simulate the multi-line behavior of UILabel in an OSX application. By drawing a multi-line shortcut, you can specify the number of lines, and the linebreak system works great!

Here is an example of what I want to achieve.

This is the string I want to show using UILabel

Lorem Ipsum is simply dummy text of the printing and typesetting industry. 

I can select the 3 line shortcut to get tail truncation:

  Lorem Ipsum is simply dummy text of the printing and typeset... 

or with a label of 2 lines with truncation of the tail, I get:

  Lorem Ipsum is simply dummy text of the pri... 

I assume that I need a low-level API to draw a string this way.

Currently, I find 3 different ways to draw text, and I want to understand what is right for this task.


Cocoa Text

NSTextView and NSTextField: it seems that this is too high level ... I cannot reproduce this effect (NSTextView does not allow me to select the number of lines, and line break seems to convert a multi-line NSTextView into a single line NSTextView).

Edit I thought to get the frame from the line, then get the lines from the frame and rebuild the frame using only the desired lines. What do you think?


Main text

I have great control over a text drawing using frames, but ... I cannot find a way to determine how many lines to display and how to truncate a line.


NSLayoutManager

This is also a good solution, but I do not know how to reproduce the multi-sheet effect of UILabel.

Do you have any suggestions to help me in this difficult task?

+6
source share

All Articles