\ n does not work in UIlabel

I saw similar questions here, but still I can understand why it does not work.

How to add line break (in other words add a new paragraph) in multi-line UIlabel?

I have a shortcut with lots of text,

lbl.numberOfLines = 0; lbl.sizeToFit; 

but I still get something like this: "Some texts are here \ nAnd here I want a new line"

thanks

+7
objective-c iphone uilabel newline
source share
12 answers

UILabel will not respect \ n, you can use the return option (in the interface builder) to force the place where you want to split the line.

You can use UIWebView instead of a label, and then you can format as you like. (And set the lineBreakMode parameter as described above in AngeDeLaMort.)

+14
source share

This is an old question, but just wanted to tell you that \r works like a charm :)

+19
source share

You can try the following:

 lbl.text = @"My \n label"; lbl.numberofLines = 0; 
+6
source share

This should work:

 lbl.lineBreakMode = NSLineBreakByWordWrapping; lbl.numberOfLines = 0; 
+4
source share

there really is a way to do this. try

  unichar chr[1] = {'\n'}; NSString *singleCR = [NSString stringWithCharacters:(const unichar *)chr length:1]; [yourLabel setText:[NSString stringWithFormat:@"new%@line",singleCR]]; 
+3
source share

If your line is ok, maybe you can also try adding this line:

 lbl.lineBreakMode = UILineBreakModeWordWrap; 
+1
source share

Your problem may be different ... but mine was the literal character \n , which is actually \\n in memory.

I solved this with:

 label.text = [rawText stringByReplacingOccurrencesOfString@"\\n" withString:@"\n"]; 

You still need to set the line break mode as well as the numerical functions to work.

+1
source share

use ctrl + Enter in the storyboard and make the number of lines as 0

+1
source share

I know the old question, but I was wondering if the problem could be lbl.sizeToFit; ?

if you can set the frame as CGRectMake (0,0,300,300) - does this solve the problem? Because \n works on my side.

0
source share
 NSCharacterSet *charSet = NSCharacterSet.newlineCharacterSet; NSString *formatted = [[unformatted componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@"\n"]; 
0
source share

UILabel uses \ n to create a new line. \ n need a seat in the back.

0
source share

This is an old question, but if someone wants to make builder.then from the interface, it's easy and simple.

select a shortcut and change type assigned by default: normal

enter image description here

now where you want \ n just use Alt + Enter and you will get a new line

hope this is useful for anyone using an interface builder to achieve this

0
source share

All Articles