Force UILabel to display 2 lines

14,928

Solution 1

Step 1. Set number of lines to your label to 2.
Step 2. Set "Vertical hugging priority" to High (1000)
Step 3. Right click on your label and connect with icon at left and choose "Center Vertically".
Step 4. Right click on your label and connect with icon at left and set "Horizontal Spacing".
Step 5. If that text is static then do as suggested Igor, else, just replace @" " with @"\n" after assigning text to that label. E.g.

NSString *text = @"Какой-то Справочник"; 
self.myLabel.text = [text stringByReplacingOccurrencesOfString:@" " withString:@"\n"];

Or just set self.myLabel.lineBreakMode = NSLineBreakByWordWrapping;

Solution 2

In InterfaceBuilder settings of UILabel text press option+enter between "Справочник" and "МКБ".

Or set label.text = @"Справочник\nМКБ";.

And, of course, set yourLabel.numberOfLines = 2 or 0 in IB or code.

Solution 3

Following code may helps you and you have to make label's height enough to cover two lines.

NSString *text = @"Какой-то Справочник";
self.Label.lineBreakMode = NSLineBreakByWordWrapping;
self.Label.numberOfLines = 2;

Solution 4

You need to set the numberOfLines.

yourLabel.numberOfLines = 2

Solution 5

you can either force the label to present a two line text with inserting a "\n" in the location you want in the string.

Or you can set a constant width to the label to force it to stay in a constant width always (this will cause the label to present the text in two rows, as long as the text is bigger then the width). Remember to set the allowed rows number to 2.

Share:
14,928
Evgeniy Kleban
Author by

Evgeniy Kleban

Updated on June 25, 2022

Comments

  • Evgeniy Kleban
    Evgeniy Kleban almost 2 years

    I have UILabel that display single line if it have space for it.

    However, I need to force it always display 2 lines, even if there is enough space for display it in 1 line.

    Robust way is to reduce right spacing constraint and check that condition on all of available devices, but maybe there is an easier way?

    To be clear, I want this:enter image description here

    look like this: enter image description here

    As I mentioned, now labels binded to superview by constraints.

  • Nikolaj Simonsen
    Nikolaj Simonsen almost 8 years
    This will not force two lines, but enable two lines.
  • user3182143
    user3182143 almost 8 years
    This is what op wants.