在我的應用程序中,我必須從網上加載的webPage更改字體。如何在iOS中的UIWebView的文檔目錄中使用Javascript?
首先,我將我的CustomJava.js文件存儲在在線託管中,並使用該文件更改iOS應用中的字體。
這是我的一些代碼。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *js = [NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.src = 'http://www.gustohei.com/SYjs/customJava.js';"];
js = [NSString stringWithFormat:@"%@ document.getElementsByTagName('head')[0].appendChild(script);", js];
[self.webViewOfBrowser stringByEvaluatingJavaScriptFromString:js];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
}
這很好,正確地更改字體。
在我的情況下,我想保留在我的文檔目錄中的JS文件,並希望從文檔目錄中使用該文件。我不想從網上使用。
所以我寫了下面的代碼。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *jsPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"customJava.js"];
NSString *js = [NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.src ='%@'",jsPath];
js = [NSString stringWithFormat:@"%@ document.getElementsByTagName('head')[0].appendChild(script);", js];
[self.webViewOfBrowser stringByEvaluatingJavaScriptFromString:js];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
}
但是這是行不通的。 如何做到這一點?
這不是工作兄弟。我像你說的那樣將相同的JS文件加載到Bundle中。但字體不會像當我使用來自Online的JS文件時那樣改變。 – 2013-02-14 08:12:42
此鏈接可能會幫助你然後http://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources – iphonic 2013-02-14 08:13:52