Reputation: 2985
I have a UILabel already with its line break mode setted to truncate tail. The problem is that I have a string that has no line breaks.
Is there an easy way that the UILabel adds break lines to the string??
Example: "This is a long string that fits in 2 lines" --adding break lines--> "This is a long string \nthat fits in 2 lines"
Or do I have to make a function that calculates, given a width and break mode, where to insert break lines?
Thanks!
Upvotes: 2
Views: 6764
Reputation: 2985
I think that the best answer is that you shouldn't be taking care of break lines. Just give the text to UILabel and it will break it correctly according to its break line mode.
The important thing is to remember that UILabel won't resize it self. You have to increase its height if necessary and then assign the text. Or do both things overriding the setText: method.
Upvotes: 0
Reputation: 11818
add a \r\n into the string.
This in Character 10 and character 13 in succession.
This is actually a message to the system that a Carriage Return and a line feed has been issued. And that will tell it to put the text on the next line :)
You also have to tell the label to allow multiple lines.
UILabel *label; // We will assume this label exists.
label.numberOfLines = 3;
label.text = @"This String Breaks Here -->\r\nThis is on the next line";
the label will have 2 lines even tho i set it to 3. because i only have one line break... Unless the text wraps, and at that point one line would wrap. and the other would truncate in the middle.
Upvotes: 3
Reputation: 13381
UITextView
is probably a better bet for easy multi-line text. Just set the editable
property to NO
if you want it readonly.
Upvotes: 0