0

I would like to have long 'NSString' with this html:

<html>

    <head>
       <title>Title of the document</title>
    </head>

    <body>
       <h1>My First Heading</h1>
    </body>

</html>

Is there a comfortable way of doing this? Something like:

    NSString *a = [NSString stringWithLongString: "   

<html>

    <head>
       <title>Title of the document</title>
    </head>

    <body>
       <h1>My First Heading</h1>
    </body>

</html>"]

Without having to sqwush everything to one line?

Hemant Singh Rathore
  • 2,153
  • 1
  • 24
  • 38
Luda
  • 7,282
  • 12
  • 79
  • 139
  • Consider putting the html text in its own file. You can simply load it with +stringWithContentsOfFile:encoding:error: – w-m Feb 21 '13 at 12:48
  • What if I need to change the content of the html constantly? – Luda Feb 21 '13 at 12:55
  • @Luda you can add the .html file to Xcode like any other source code file and edit it there. It will even have correct syntax coloring. – w-m Feb 21 '13 at 13:35
  • But, I need it to be done on run time – Luda Feb 21 '13 at 14:18

2 Answers2

2

Yes, you can add a \ to the end of every line:

NSString *a = @"    <html>\
\
    <head>\
    <title>Title of the document</title>\
    </head>\
\
    <body>\
    <h1>My First Heading</h1>\
    </body>\
\
</html>";

There is no need to do a [NSString stringWithString:] method, also there is not method stringWithLongString: for NSString.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Even if this is correct, putting the html into a file would be much better solution than polluting code by html. – Sulthan Feb 21 '13 at 12:53
  • True, but that is not the question asked by @Luda. I do agree that putting it in a separate file would be beter. – rckoenes Feb 21 '13 at 12:54
  • question asked by @Luda :) And what if I need to change the content of the html constantly? – Luda Feb 21 '13 at 12:57
0

End each line with a "\"

NSString *a = @"    <html>\
\
<head>\
<title>Title of the document</title>\
</head>\
\
<body>\
<h1>My First Heading</h1>\
</body>\
\
</html>";
Shashank
  • 1,743
  • 1
  • 14
  • 20