101

Does anyone know how to use line breaks in NSString? I need to do something like this -

[NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2];
Dave
  • 4,038
  • 9
  • 45
  • 57
  • 11
    Did you actually try this? Because if you had, you would've gotten your answer. – Dave DeLong Mar 17 '10 at 01:09
  • no it didn't work. i see the \n appended to the string but when it renders, it is on the same line. – Dave Mar 17 '10 at 16:20
  • 4
    Please make sure this NSString type message you are displaying not using HTML support. If using HTML support, it will not treat \n . Hope i am helpful. Thanks, Aby – Abdul Yasin May 21 '14 at 09:47

10 Answers10

180

I just ran into the same issue when overloading -description for a subclass of NSObject. In this situation I was able to use carriage return (\r) instead of newline (\n) to create a line break.

[NSString stringWithFormat:@"%@\r%@", mystring1,mystring2];

Anthony F
  • 6,096
  • 4
  • 31
  • 32
32

If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0

myView.numberOfLines=0;
Andy A
  • 4,191
  • 7
  • 38
  • 56
26

I found that when I was reading strings in from a .plist file, occurrences of "\n" were parsed as "\\n". The solution for me was to replace occurrences of "\\n" with "\n". For example, given an instance of NSString named myString read in from my .plist file, I had to call...

myString = [myString stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];

... before assigning it to my UILabel instance...

myLabel.text = myString;
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
12
NSString *str1 = @"Share Role Play Photo via Facebook, or Twitter for free coins per photo.";
NSString *str2 = @"Like Role Play on facebook for 50 free coins.";
NSString *str3 = @"Check out 'What's Hot' on other ways to receive free coins";


NSString *msg = [NSString stringWithFormat:@"%@\n%@\n%@", str1, str2, str3];
Alex Zavatone
  • 4,106
  • 36
  • 54
Mohit
  • 3,708
  • 2
  • 27
  • 30
8

try this

 [NSString stringWithFormat:@"%@\n%@",string1,string2];
mfaani
  • 33,269
  • 19
  • 164
  • 293
2

\n\r seems working for me.

I am using Xcode 4.6 with IOS 6.0 as target. Tested on iPhone 4S. Try it by yourself.

Feng Chiu

us_david
  • 4,431
  • 35
  • 29
2

Line breaks character for NSString is \r

correct way to use [NSString stringWithFormat:@"%@\r%@",string1,string2];

\r ----> carriage return

Shebuka
  • 3,148
  • 1
  • 26
  • 43
Chamath Jeevan
  • 5,072
  • 1
  • 24
  • 27
1

\n is the preferred way to break a line. \r will work too. \n\r or \r\n are overkill and may cause you issues later. Cocoa, has specific paragraph and line break characters for specific uses NSParagraphSeparatorCharacter, NSLineSeparatorCharacter. Here is my source for all the above.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
mahboudz
  • 39,196
  • 16
  • 97
  • 124
1

In case \n or \r is not working and if you are working with uiwebview and trying to load html using < br > tag to insert new line. Don't just use < br > tag in NSString stringWithFormat.

Instead use the same by appending. i.e by using stringByAppendingString

 yourNSString = [yourNSString stringByAppendingString:@"<br>"];
iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23
0

In Swift 3, its much simpler

let stringA = "Terms and Conditions"
let stringB = "Please read the instructions"

yourlabel.text =  "\(stringA)\n\(stringB)"

or if you are using a textView

yourtextView.text =  "\(stringA)\n\(stringB)"
Naishta
  • 11,885
  • 4
  • 72
  • 54