Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word Wrap not working for UILabel

Using autolayout I can't override my label in code. I've set the labels attributes in IB: Lines = 0, LineBreaks = Word Wrap, but I have my height set to a single line because due to what cell is selected determines what text goes in the label. So sometimes the label will only have one line.

In my viewDidLoad:

myLabel.text = @”blah, blah, blah….”;
[myLabel setLineBreakMode:NSLineBreakByWordWrapping];
myLabel.numberOfLines  = 0; //have tried 1 but didn’t help
[myLabel sizeToFit];

This works on another project, but I wasn’t using AutoLayout. AutoLayout seems to override these settings.
I’ve even added

[myLabel setFrame:CGRectMake(20, 135, 280, 80); 

but it doesn’t help.

like image 923
KFP Avatar asked Aug 05 '14 15:08

KFP


1 Answers

Allow the intrinsic size of the label determine the height. You are correct that you need to set the numberOfLines property to 0. Since you are using AutoLayout, don't call sizeToFit and you need set the preferredMaxLayoutWidth of the label.

like image 158
Justin Moser Avatar answered Sep 28 '22 07:09

Justin Moser