Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple lines of text in UILabel in iOS 9

I`m working on an iOS project but when I updated to iOS 9 I had some problem with multiline in UILabels. I'm using Autolayout.

Anybody knows how to do it in iOS 9 ?

I tried different ways such as:

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;

(from other similar question) but it did not work.

This is my logic to show multilines:

IB config:

preferedMaxLayout

enter image description here

label config

I update this value programmatically:

- (void)configureLabelsMaxLayoutWidth
{
     [self.view layoutIfNeeded];
     self.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.titleLabel.frame);
}

I call this method on the viewWillAppear

like image 579
eljec Avatar asked Sep 28 '15 13:09

eljec


1 Answers

By setting numberOfLines to 0 you're telling the label to be multiline, however there may be other factors disabling multilines. Are you using autolayout? If so, that problem may be that the labels Content Compression Resistance Priority is too low, try setting it to Required or 1000.

The content compression resistance tells the view engine at what priority your label can shrink. Setting it to required forces it to not shrink.

In Interface Builder, simply select the label, tap Size Inspector (the little ruler) and then change it to 1000.
enter image description here

Or, in code, the equivalent would be:

[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
like image 191
Simon Avatar answered Sep 22 '22 01:09

Simon