我有一个html字符串,如下所示:
<html> <head> <title>some text</title> <link rel="stylesheet" href="http://www.xyz.com/css/main.mobile.css" /> ...文本本身包含双引号,所以我如何加载到NSString中,因为我不能这样做;
NSString *html = @" "将html字符串放在双引号中。我的目标是将html字符串加载到UIVewView中。提前谢谢。
发布于 2014-04-09 21:38:29
如果您使用的是字符串文字,则只需使用反斜杠对双引号进行转义,如下所示:
NSString *html = @"<html> <head> <title>some text</title> <link rel=\"stylesheet\" ...";发布于 2014-04-09 21:40:13
你可以在每个引号前面加一个反斜杠。
NSString *test = @"lorem\"ipsum\"do...";或者将超文本标记语言放在一个单独的文件中,并将其加载到NSString中
// get a reference to our file
NSString *myPath = [[NSBundle mainBundle]pathForResource:@"myfile" ofType:@"html"];
// read the contents into a string
NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];
// display our file
NSLog("Our file contains this: %@", myFile);发布于 2014-04-09 21:35:41
htmlStr = [htmlStr stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];THen加载到THen上
[self.webView loadHTMLString:htmlStr baseURL:nil];https://stackoverflow.com/questions/22964122
复制相似问题