Is there any way to declare a NSString
in multiple lines? I want to write HTML code and store it into a NSString
, and in multiple lines de code will be more readable. I want to do something like this:
NSString *html = @"\<html\>"
+ @"\<head\>"
+ @"\<title\>The Title of the web\</title\>"
+ @"\</head\>"
+ @"\<body\>"
[...]
Using Backslash If we place a backslash at the end of each line, the compiler removes the new-line and preceding backslash character. This forms the multiline string.
Long strings can be written in multiple lines by using two double quotes ( “ “ ) to break the string at any point in the middle.
There are three ways to create a multiline string in JavaScript. We can use the concatenation operator, a new line character (\n), and template literals. Template literals were introduced in ES6. They also let you add the contents of a variable into a string.
This is an example:
NSString *html = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%@\"; font-size: %dpx;}\n"
"img {max-width: 300px; width: auto; height: auto;}\n"
"</style> \n"
"</head> \n"
"<body><h1>%@</h1>%@</body> \n"
"</html>", @"helvetica", 16, [item objectForKey:@"title"], [item objectForKey:@"content:encoded"]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With