Does anyone know how to use line breaks in NSString? I need to do something like this -
[NSString stringWithFormat:@\"%@,\\n%@\", mystring1,mystring2];
\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
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;
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;
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];
\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.
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>"];