How to write multiple lines in a label
Solution 1
You could use a TextView and disable the Editable and User Interaction Enabled options in IB.
Solution 2
use this
myLabel.lineBreakMode = UILineBreakModeWordWrap;
if you are worried about backwards compatibility use this because the comand i mentioned before was introduced in iOS 6.0
myLabel.numberOfLines = 0;
Solution 3
I have been running into a similar problem. I started out with a label, but wanted multi-lines and a UITextView
worked great for this, at first. Because I am using a third party library (MBProgressHUD) to handle stuff like progress messages and what not, I had thread problems when trying to update a UITextView
and show the progress message at the same time.
So I went back to UILabel
which actually didn't have thread problems. I found a property to allow a specific number of lines of my choosing, and had to create the label big enough to display those lines. The downfall to this approach is that I don't get the context menus like Copy, Paste, etc. but more importantly I'm not running into thread problems. I can always embed this into a UIScrollView
in the future if I so choose.
Solution 4
You could check out my category for UILabel on GitHub. https://gist.github.com/1005520
This lets you resize the height of the label to accommodate the content.
Solution 5
From the Attiribute inspector, choose LineBreaks->Word Wrap option when you have selected the lablel. Also increase the number of lines Label->Lines.
Lea Cohen
Web developer, both server-side and client-side. Started my web career with ASP.NET and now also write in PHP (mainly Wordpress, some Moodle). SOreadytohelp
Updated on December 18, 2020Comments
-
Lea Cohen over 3 years
I have a XIB file of type View XIB. In it I have a View controller, and in the View controller I have a label.
I have long text that I want to break, but it doesn't - it gets truncated.
I tried this solution - I did it in the attribute window - but it doesn't help. -
Lea Cohen about 12 yearsWhat property did you find that allowed you to specify the number of rows?"
-
Boeckm about 12 yearsThere should be a property in the designer of XCode to specify the number of lines you would like on the UILabel. (I don't have XCode in front of me and can't check right now.) You should also be able to set the property of the label on load, something like textLabel.numberOfLines = 3;