NSString with \n or line break

2019-01-13 00:38发布

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

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

10条回答
SAY GOODBYE
2楼-- · 2019-01-13 00:57

\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

查看更多
神经病院院长
3楼-- · 2019-01-13 01:05

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;
查看更多
【Aperson】
4楼-- · 2019-01-13 01:07

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;
查看更多
何必那么认真
5楼-- · 2019-01-13 01:10

Line breaks character for NSString is \r

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

\r ----> carriage return

查看更多
登录 后发表回答