Declare a NSString in multiple lines
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 开发者_StackOverflow中文版will be more readable. I want to do something like this:
NSString *html = @"\<html\>"
+ @"\<head\>"
+ @"\<title\>The Title of the web\</title\>"
+ @"\</head\>"
+ @"\<body\>"
[...]
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"]];
I know it's Objective-C question but it's pretty easy with Swift:
let multiline = """
first line
second line
without escaping characters
""";
NSString * html = @"line1\
line2\
line3\
line4";
And beware of tabs/spaces in the beginning of each line - they do count in this case.
Create an NSString with multiple lines to mimic a NSString read from a multi-line text file.
NSString * html = @"line1\n\
line2\n\
line3\n\
line4\n";
A \n on the last line is up to you.
精彩评论