2

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!

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I always thought than \n was enough, at least in iOS... But can't check right now. LF/CR sounds more bullet-proof, though.
You are correct sir. \n is enough. but for completeness \r\n has always been my standard. \r is line feed. \n is carriage return. unix and the like operating systems interpret \r as both and sometimes \r is omitted. but I originated from a windows background so Im used to using both. Thanks for pointing that out tho. :)
I might have those backwards. But you get the idea. \r = 0x0A \n = 0x0D and in most line breaks (at least in hexadecimal) you will see them in succession. 0x0A0D = \r\n
@VolureDarkAngel Just to clarify, carriage return (CR) is \r, 0x0D, and line feed (LF) is \n, 0x0A. Thus the typical Windows "carriage return, line feed" is \r\n. If viewed in byte order (word order is a whole different kettle of fish), that's 0x0D followed by 0x0A. For more than you ever wanted to see on this topic, refer to newline. Going back to the original observation, personally, on iOS I would advise to just use \n.
You can also use numberOfLines of zero, which allows multiline UILabel's, too.
|
0

UITextView is probably a better bet for easy multi-line text. Just set the editable property to NO if you want it readonly.

1 Comment

UITextView has a different set of issues: different line height, imposible to 'sizeToFit' until added as child view...
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.