NSString with \n or line break

前端 未结 10 816
生来不讨喜
生来不讨喜 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:50

    try this ( stringWithFormat has to start with lowercase)

     [NSString stringWithFormat:@"%@\n%@",string1,string2];
    
    0 讨论(0)
  • 2020-12-04 11:51

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

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

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

    Line breaks character for NSString is \r

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

    \r ----> carriage return

    0 讨论(0)
提交回复
热议问题