Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set maximum number of lines for UILabel

Tags:

I need to do some special alignment with a UILabel.

I want it to have 1 to 3 lines, whatever content it has.

Currently I'm doing it something like this:

label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0f]; label.backgroundColor = [UIColor clearColor]; label.numberOfLines = 0; label.lineBreakMode = UILineBreakModeWordWrap;  label.frame = CGRectMake(114.0f, 88.0f, 187.0f, 0.0f); [label sizeToFit]; 

which works great if the text is not too long. if the string is something like @"Hello World" the UILabel is only 14 points high, if it is some way longer it expands.

Now I want that the text should add it's default ... triple dots if the text gets too long for three lines, but with the setting on top it expands to the fourth line.

How do I achieve this?

like image 956
choise Avatar asked Nov 19 '11 23:11

choise


People also ask

How do you control line spacing in UILabel?

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."

How do I change my UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

What is UILabel?

A view that displays one or more lines of informational text.


1 Answers

Just set label.numberOfLines = 3;.

It is somewhat mislabeled, as it actually holds the maximum amount of lines to display.

like image 170
Gil Avatar answered Oct 06 '22 23:10

Gil