Simulating UILabel behaviour on OSX
Asked Answered
S

0

6

I want to simulate the multi-line UILabel behaviour in an OSX application. Drawing a multiline Label you can specify the number of rows and the linebreak system works great!

Here an example of what I want to achieve.

This is the string that I want to show with a UILabel

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

I can select a 3 lines label with tail truncation obtaining:

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

or with a 2 lines label with tail truncation i get:

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

I suppose that I need a low level API to draw a string in that way.

Currently I find 3 different ways to draw text and I want to understand which is the right one to accomplish this task.


Cocoa Text

NSTextView and NSTextField: It seems to be a too high level... I can't reproduce this effect (The NSTextView doesn't let me choose the number of rows, and the line break seems to transform the multi line NSTextView in a single line NSTextView).

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


Core Text

I have great control of the text drawing using frames, but... I can't find a way to define how many rows to display and how to truncate the string.


NSLayoutManager

It seems to be a good solution too, but I don't know how to reproduce the UILabel multiline effect.

Do you have some suggestions to help me in this tricky task?

Stalkinghorse answered 7/1, 2013 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.