有没有办法在多行中声明NSString
?我想编写HTML代码并将其存储到NSString
中,并且在多行中代码将更具可读性。我想做这样的事情:
NSString *html = @"\<html\>"
+ @"\<head\>"
+ @"\<title\>The Title of the web\</title\>"
+ @"\</head\>"
+ @"\<body\>"
[...]
答案 0 :(得分:38)
这是一个例子:
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"]];
答案 1 :(得分:0)
我知道这是Objective-C问题,但是使用Swift很简单:
let multiline = """
first line
second line
without escaping characters
""";
答案 2 :(得分:0)
NSString * html = @"line1\
line2\
line3\
line4";
并注意每行开头的制表符/空格 - 在这种情况下它们确实很重要。