Question

I have a view that draws multicolored text inside UITableViewCell. To draw multicolored text I'm using NSAttributedString However, I would like to make it so that if the text is too long to fit into the view, the last visible line is truncated to display an ellipsis at its end.

Obviously this is very easy to do when drawing only a single line, as you can just set kCTLineBreakByTruncatingTail for the line break mode of the paragraph style. The problem is that I want my text to wrap to fill the rectangle, and then only have the last line truncated with an ellipsis - setting the line break mode confines the whole text to one line.

Does anybody have any ideas of how I would go about this?

Many thanks in advance for any suggestions, JC.

Was it helpful?

Solution 3

Well, to activate text truncating in UILabel, you should re-set lineBreakMode parameter to NSLineBreakByTruncatingTail after setting attributedText.

 textLabel.attributedText = attributedText;
 textLabel.lineBreakMode = NSLineBreakByTruncatingTail;

OTHER TIPS

Create your CTFrame from your CTFrameSetter with the rectangle of your UITableViewCell. Then, you can get all the CTLines of your CTFrame and determine when they will cut off. To swap out the ellipsis, you could keep that drawn with a separate CTFrame and draw it over the overflowing text on the last line.

You can find working code here: https://stackoverflow.com/a/14612598/473067

It's a similar approach to what Heath suggested. But then all wrapped up in a shiny package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top