views:

1455

answers:

4

Let see that I have a string look like this:

NSString *longStr = @"AAAAA\nBBBBB\nCCCCC";  

How do I make it so that the UILabel display the message like this

AAAAA
BBBBB
CCCCC

I dont think, '\n' recognize by UILabel, so is there anything that I can put inside NSString, so that UILabel know that it has to create a line break there? Thank you very much in advance

A: 

Try \r\n

Works most of the time for everything

Sorry if this is not the best way of doing it, but i've had similar problems in the past and it was because of the \r.

I've never programmed for an IPhone so don't know any properties.

LnDCobra
\r\n is for non UNIX OSs (DOS, Windows), \n is for UNIX OSs (iPhone, Mac, Linux, etc...) Using \r\n on the iPhone will not add a line break for a UILabel.
Gerry
Its the other way around ;) \n for windows, \r\n for non Unix ;) hence why it probably did work. I didn't know about numberOfLines property, but thought id post this as a quick fix.
LnDCobra
+13  A: 

You have to set the numberOfLines property on the UILabel. The default is 1, if you set it to 0 it will remove all limits.

Dan
+4  A: 

Use \n as you are using in your string.

Set numberOfLines to 0 to allow for any number of lines.

label.numberOfLines = 0;

Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.

UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
                          constrainedToSize:label.frame.size
                              lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
    label.origin.x, label.orgin.y, 
    label.size.width, labelSize.height);
Gerry
A: 

Important to note it's \n (backslash) rather than /n

AndyOng