Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set adaptive multiline UILabel text?

Tags:

ios

swift

I have a UILabel named titleLabel in my storyboard nib set to its default height. I want it to programatically expand in height to fit it's content. Here is what I have tried so far:

// just setting content titleLabel.text = "You don't always know what you are getting with mass-market cloud computing services. But with SimpliCompute, the picture is clear. SimpliCompute gives you powerful virtual servers you can deploy using just your web browser. That’s enterprise grade technology you can deploy and control on-the-fly."  titleLabel.numberOfLines = 0  titleLabel.preferredMaxLayoutWidth = 700  titleLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping  titleLabel.sizeToFit() 

None of this works for me in any combination! I always only see one line of text in my UILabel. What am I doing wrong?

I absolutely need the text content to be variable.

like image 593
Amit Erandole Avatar asked Jun 10 '14 07:06

Amit Erandole


People also ask

What is UILabel in Swift?

A view that displays one or more lines of informational text.


1 Answers

I kind of got things working by adding auto layout constraints:

auto layout contraints

But I am not happy with this. Took a lot of trial and error and couldn't understand why this worked.

Also I had to add to use titleLabel.numberOfLines = 0 in my ViewController

like image 172
Amit Erandole Avatar answered Sep 18 '22 15:09

Amit Erandole