Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel attributedText with multiple line break modes

I have a requirement of showing a UILabel with text that has two different styles (different colours, parts of the text bolded). This is solved easily enough by using the attributedText-property.

My problem is that the text may or may not be longer than what I can fit in my label. When using plain text everything works the way I want it to. The text is word wrapped to fit the number of lines in the label and the tail is truncated if/when the text is longer than can be shown in the label.

When I switch to using attributedText I am only able to choose between tail truncation and word wrapping. If I want the tail truncated the label only renders a single line with the truncated tail (even though it could fit 10 lines). If I choose word wrapping then the tail is not truncated but the lines that cannot fit in the label are simply not shown.

My content string does not contain any line breaks, it is simply one long string.

like image 655
Ville Rinne Avatar asked May 30 '13 08:05

Ville Rinne


People also ask

How to break line in UILabel?

Method Three − Programmatically adding line breaks in the text. To add line breaks in the text we'll use \n character in the string we want to assign to the label.

What is line break mode?

The mode for breaking lines in the paragraph that don't fit within a container.


1 Answers

try this:

[_text drawWithRect:_textRect options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil];

like image 122
pikachu Avatar answered Oct 05 '22 23:10

pikachu