[英]How to load html string which contains double quotes into UIWebView
我有一個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中。 提前致謝。
如果您使用的是字符串文字,那么您只需要使用反斜杠轉義雙引號,如下所示:
NSString *html = @"<html> <head> <title>some text</title> <link rel=\"stylesheet\" ...";
你可以在每個報價前加一個反斜杠
NSString *test = @"lorem\"ipsum\"do...";
或者將HTML放在一個單獨的文件中並將其加載到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);
htmlStr = [htmlStr stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
然后加載到webwiew中
[self.webView loadHTMLString:htmlStr baseURL:nil];
另一種方法是用單引號替換字符串中的所有雙引號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.