290

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 don't think \n is recognized by UILabel, so is there anything that I can put inside NSString so that UILabel knows that it has to create a line break there?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Thang Pham
  • 38,125
  • 75
  • 201
  • 285

21 Answers21

346

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.frame.origin.x, label.frame.origin.y, 
    label.frame.size.width, labelSize.height);

Update : Replacement for deprecated

sizeWithFont:constrainedToSize:lineBreakMode:

Reference, Replacement for deprecated sizeWithFont: in iOS 7?

CGSize labelSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];

label.frame = CGRectMake(
    label.frame.origin.x, label.frame.origin.y, 
    label.frame.size.width, labelSize.height);
Community
  • 1
  • 1
Gerry Shaw
  • 9,178
  • 5
  • 41
  • 45
  • 2
    `UILabel` doesn't have a `origin` nor `size` property on iOS? It should probably be `label.frame.origin.x` etc. – Peterdk Mar 26 '11 at 10:58
  • 1
    Instead of using sizeWithAttributes then setting the frame, you can just call sizeToFit to work-out and set the frame size in one quick step. – jimmyjudas Aug 13 '15 at 08:37
  • @Hermang, thanks for editing the answer to bring it up to date. – Gerry Shaw Nov 02 '15 at 22:29
296

enter image description here

Use option-return when typing in the little box in Interface Builder to insert a line feed (\n). In Interface Builder's Label attributes, set # Lines = 0.

Select the label and then change Lines property to 0 like in the above image, and then use \n in your string for line break.

Marco
  • 6,692
  • 2
  • 27
  • 38
  • 1
    Does not work when setting label programmatically and adding \n (newline) to the string. – Chewie The Chorkie Jan 30 '18 at 18:51
  • amazing. My requirement was to do this with storyboard label only. And this worked like a charm. – Tejas Mar 22 '18 at 12:55
  • NOTE: If you are setting in the storyboard label, and you're still having issues, make sure the label height is long enough to account for two lines. Even if it properly displays before building, it might have insufficient height upon loading – craft Nov 18 '18 at 21:38
158

In the interface builder, you can use Ctrl + Enter to insert /n to the position you want. This way could implement the following situation

aaa
aaaaaaa

m4n0
  • 29,823
  • 27
  • 76
  • 89
kuokuo321
  • 1,603
  • 1
  • 10
  • 11
136

If you read a string from an XML file, the line break \n in this string will not work in UILabel text. The \n is not parsed to a line break.

Here is a little trick to solve this issue:

// correct next line \n in string from XML file
NSString *myNewLineStr = @"\n";
myLabelText = [myLabelText stringByReplacingOccurrencesOfString:@"\\n" withString:myNewLineStr];

myLabel.text = myLabelText;

So you have to replace the unparsed \n part in your string by a parsed \n in a hardcoded NSString.

Here are my other label settings:

myLabel.numberOfLines = 0;
myLabel.backgroundColor = [UIColor lightGrayColor];
myLabel.textColor = [UIColor redColor]; 
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];   
myLabel.textAlignment = UITextAlignmentCenter;

Most important is to set numberOfLines to 0 (= unlimited number of lines in label).

No idea why Apple has chosen to not parse \n in strings read from XML?

starball
  • 20,030
  • 7
  • 43
  • 238
Al-Noor Ladhani
  • 2,413
  • 1
  • 22
  • 14
  • 1
    iOS doesn't parse `\n` when getting strings from Parse.com (I guess it will also happen when getting string from other APIs) – Brian May 01 '15 at 14:34
  • You saved my day. I'm getting string with newline from xml files. – Senry May 09 '17 at 11:29
26

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
  • 17,375
  • 3
  • 36
  • 39
24

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

AndyOng
  • 241
  • 2
  • 2
17

For those of you who want an easy solution, do the following in the text Label input box in Interface Builder:

Make sure your number of lines is set to 0.

Alt + Enter

(Alt is your option key)

Cheers!

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
Derwrecked
  • 771
  • 1
  • 6
  • 17
12

In Swift 2.2, > iOS 8

I've set Lines = 0 on Storyboard, under Attribute Inspector and linked a referencing outlet to the label. Then use in controller like this:

 @IBOutlet weak var listLabel: UILabel!

 override func viewDidLoad() {
      ...
      listLabel.text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8"
 }
CodeOverRide
  • 4,431
  • 43
  • 36
9

In xCode 11, Swift 5 the \n works fine, try the below code:

textlabel.numberOfLines = 0
textlabel.text = "This is line one \n This is line two \n This is line three"
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
David_2877
  • 257
  • 4
  • 7
8

Just do it like this

NSString * strCheck = @"A\nB";

strCheck = [strCheck stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];  //This is to prevent for fetching string from plist or data structure

label.numberOfLines = 0;

label.lineBreakMode = NSLineBreakByWordWrapping;

label.text = strCheck;
Serge Seredenko
  • 3,541
  • 7
  • 21
  • 38
Sandip Patel - SM
  • 3,346
  • 29
  • 27
6

// DO not forget to set numberOfLines to zero

UILabel* locationTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 30, 230, 40)];
locationTitle.font = [UIFont systemFontOfSize:13.0];
locationTitle.numberOfLines = 0;
locationTitle.text = [NSString stringWithFormat:@"Eaton industries pvt. Ltd \nUK Apr 12"];
[cell addSubview:locationTitle];
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
4

If your using a UILabel you have to remember that the default setting is 1 line, so it does not matter how many breaks you add (\n or \r), you need to make sure it is set to more than one line so it could be allowed to append more lines.

One alternative is to use UITextView which is really meant for multilines.

You can easily achieve this in XCode attribute section of the UILabel, see screenshot:

enter image description here

meda
  • 45,103
  • 14
  • 92
  • 122
  • If you set the number of lines to 0 then you are guaranteed unlimited number of lines, so that if your code changes in the future to say... beyond 3 lines, then you will not run into problems. It's unknown why Apple took this approach. – David_2877 Apr 23 '21 at 07:52
4

On Xcode 6, you can just use \n even inside a string when using word wrap. It will work. So for example:

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, screenRect.size.width, 50)];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"This will be on the first line\nfollowed by a line under it.";
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
Roi
  • 51
  • 5
3

In my case also \n was not working, I fixed issue by keeping number of lines to 0 and copied and pasted the text with new line itself for example instead of Hello \n World i pasted

Hello

World

in the interface builder.

Ranjithkumar
  • 697
  • 6
  • 16
2

Just using label.numberOfLines = 0;

Fry
  • 6,235
  • 8
  • 54
  • 93
1
textLabel.text = @"\nAAAAA\nBBBBB\nCCCCC";
textLabel.numberOfLines = 3; \\As you want - AAAAA\nBBBBB\nCCCCC
textLabel.lineBreakMode = UILineBreakModeWordWrap;
NSLog(@"The textLabel text is - %@",textLabel.text);
user3182143
  • 9,459
  • 3
  • 32
  • 39
0

For anyone else that might have trouble with sizeWithFont:constrainedToSize:lineBreakMode: or anyone switching to ios8 (the method is deprecated as of ios7), I adjusted my height by using sizeToFit instead.

UILabel *label;
label.numberOfLines = 0;
// Setup label with desired settings
...
[label sizeToFit];
label.frame = CGRectMake(label.frame.origin.x,     // Or use any desired origin
                         label.frame.origin.y, 
                         label.frame.size.width,   // Or use any desired width
                         label.frame.size.height);
0
NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet;
NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@"\n"];
Grigori A.
  • 2,628
  • 1
  • 21
  • 19
0

It seems wrong to me to change the label frame sizes especially when using autolayout. Using the appendFormat method seems more appropriate. Here is my example:

NSMutableString *list = [[NSMutableString alloc] init];
NSArray *textArray = @[@"AAAA", @"BBBB"];
for (NSString *string in textArray) {
    [list appendFormat:@"%@\n", string.mutableCopy];
}
self.label.text = list;
self.label.numberOfLines = 0;
tanya
  • 117
  • 7
0

If you set your UILable properties from Plain to Attributed...the UILabel will hold multiline text no matter how many paragraphs for along as your UILabel height and width are set to fit the screen area you want to display the text in.

0

I have faced same problem, and here is, how i solved the problem. Hope this will be helpful for someone.

// Swift 2

   lblMultiline.lineBreakMode = .ByWordWrapping // or use NSLineBreakMode.ByWordWrapping
   lblMultiline.numberOfLines = 0 

// Objective-C

  lblMultiline.lineBreakMode = NSLineBreakByWordWrapping;
  lblMultiline.numberOfLines = 0;

// C# (Xamarin.iOS)

  lblMultiline.LineBreakMode = UILineBreakMode.WordWrap;
  lblMultiline.Lines = 0;  
Suraj Sonawane
  • 2,044
  • 1
  • 14
  • 24