NSString with \n or line break

Objective CNsstringNewline

Objective C Problem Overview


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

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

Objective C Solutions


Solution 1 - Objective C

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];

Solution 2 - Objective C

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;

Solution 3 - Objective C

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;

Solution 4 - Objective C

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];

Solution 5 - Objective C

try this

 [NSString stringWithFormat:@"%@\n%@",string1,string2];

Solution 6 - Objective C

\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

Solution 7 - Objective C

Line breaks character for NSString is \r

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

\r ----> carriage return

Solution 8 - Objective C

\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.

Solution 9 - Objective C

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>"];

Solution 10 - Objective C

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)"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - Objective CAnthony FView Answer on Stackoverflow
Solution 2 - Objective CAndy AView Answer on Stackoverflow
Solution 3 - Objective CAdil HussainView Answer on Stackoverflow
Solution 4 - Objective CMohitView Answer on Stackoverflow
Solution 5 - Objective Cuser3340342View Answer on Stackoverflow
Solution 6 - Objective Cus_davidView Answer on Stackoverflow
Solution 7 - Objective CChamath JeevanView Answer on Stackoverflow
Solution 8 - Objective CmahboudzView Answer on Stackoverflow
Solution 9 - Objective CiPhoneDeveloperView Answer on Stackoverflow
Solution 10 - Objective CNaishtaView Answer on Stackoverflow