NSString with \n or line break

前端 未结 10 815
生来不讨喜
生来不讨喜 2020-12-04 11:15

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

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


        
相关标签:
10条回答
  • 2020-12-04 11:41

    \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

    0 讨论(0)
  • 2020-12-04 11:42

    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;
    
    0 讨论(0)
  • 2020-12-04 11:44

    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;
    
    0 讨论(0)
  • 2020-12-04 11:47
    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];
    
    0 讨论(0)
  • 2020-12-04 11:48

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

    0 讨论(0)
  • 2020-12-04 11:48

    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>"];
    
    0 讨论(0)
提交回复
热议问题