Multiple lines of text in UILabel in iOS 9

I am working on an iOS project, but when I upgraded to iOS 9, I had a multi-line problem in UILabels. I am using Autolayout .

Does anyone know how to do this in iOS 9?

I tried different ways, for example:

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

(from another similar question), but that didn't work.

This is my logic to show multilines:

IB configuration:

preferedMaxLayout

enter image description here

label config

I am updating this value programmatically:

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

I call this method on viewWillAppear

+6
source share
3 answers

By setting numberOfLines to 0 , you say that the label is multi-line, however there may be other factors that disable multi-lines. Do you use autorun? If so, the problem may be that the Content Compression Resistance Priority labels are too small, try setting it to Required or 1000 .

Content compression resistance tells the view engine what priority your label may be shortened at. By setting it to the required forces, it does not contract.

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

Or, in code, the equivalent would be:

 [label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; 
+6
source

After setting the limits, remember preferredMaxLayoutWidth

 label.numberOfLines = 0 label.preferredMaxLayoutWidth = superview.bounds.size.width - 20 
+1
source

I had a similar problem, I needed to call layoutIfNeeded () at the end:

 textLabel.text = "your loooooooong text" textLabel.lineBreakMode = .ByWordWrapping textLabel.numberOfLines = 0 textLabel.layoutIfNeeded() 
0
source

All Articles