IOS_UIWebView字型大小、字型顏色、背景色
轉載自:http://hi.baidu.com/langezi_/archive/tag/iphone%E5%BC%80%E5%8F%91
前段時間需要修改webView背景色,上stackoverflow搜了很久沒有找到結果,百度搜索,各種轉載,各種坑爹,搜出來的都只有字型大小和字型顏色,頁面背景沒有看到,本人釋出方法,希望可以幫助到更多人
在webView的delegate回撥方法-webViewDidFinishLoad:(UIWebView*)webView;中寫上一下語句即可
//字型大小
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];
//字型顏色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
//頁面背景色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
之前把方法寫在init裡怎麼都不行 ,原來得寫在delegate裡啊 (上邊的才是正確的)
- (void)initWebView
{
if (!self.myWebView){
self.myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
}
self.myWebView.delegate = self;
self.myWebView.opaque = NO; //不設定這個值 頁面背景始終是白色
self.myWebView.backgroundColor = [UIColorclearColor];
self.myWebView.scalesPageToFit =
self.myWebView.dataDetectorTypes = UIDataDetectorTypePhoneNumber|UIDataDetectorTypeLink;
self.myWebView.scrollView.pagingEnabled = YES;
self.myWebView.scrollView.contentInset = UIEdgeInsetsMake(0, 0,0, 0);
self.myWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.myWebView];
NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",16.0,[color webColorString]];
[self.myWebViewstringByEvaluatingJavaScriptFromString:jsString];
}