Search code examples
iosuilabelautolayoutline-breaksibaction

lineBreakMode of UILabel doesn't work when linked in the storyboard


I try to do my first application for ipad/iphone with Xcode. I have created an UILabel and an IBAction in ViewController.h:

@interface ViewController : UIViewController{
    IBOutlet UILabel *display;
}

- (IBAction)ChangeText;

I linked the variable "display" with a label I created in the Main.storyboard and the method "ChangeText" to a button.

The label and the button are placed using auto layout.

The label initially contains the text "-" and when I click on the button, the method "ChangeText" change the text of the label to a longer one:

- (IBAction)ChangeText{

    [display setText:@"A long text which exceeds the size of the screen I am using. A long text which exceeds the size of the screen I am using.A long text which exceeds the size of the screen I am using."];
}

Since the text is to long for the screen I would like to display it on several lines. So I changed the lineBreakMode and the numberOfLines of "display":

- (IBAction)ChangeText{

    display.lineBreakMode = NSLineBreakByWordWrapping;
    display.numberOfLines = 0;

    [display setText:@"A long text which exceeds the size of the screen I am using. A long text which exceeds the size of the screen I am using.A long text which exceeds the size of the screen I am using."];
}

Unfortunately, the result is the same, the text is displayed on only one line.

Do you have any idea why and how I can overcome this?

Thanks in advance.


Solution

  • dehlen was right the label was not having enough space.

    I did not now that I should/could expand the size of the label manually in the storyboard. I also had to add a "Vertical spacing" constraint so that the auto layout was happy.