can*_*boy 5 objective-c uiwebview ios
我在UIView中有一个较小的UIWebView.我想将UIView的背景颜色设置为与webView加载的HTML相同.这可能吗?
我试过去,webViewDidFinishLoad得到这样的颜色:
NSLog(@"color: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.style.background"]);
Run Code Online (Sandbox Code Playgroud)
..但它没有返回任何东西.
工作代码只需三个步骤..
1)复制代码并将其粘贴到您的项目中.
2)将___webView插座连接到xib文件中的UIWebView.
3)运行.
- (void)viewDidLoad
{
[___webView loadHTMLString:[NSString stringWithFormat:@"<html><body id=\"mybody\" style=\"background-color:green;width:100px;height:100px;\"><b>Hello buddy</b></body></html>"] baseURL:nil];
___webView.delegate=self;
[super viewDidLoad];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *javaScript = @"document.body.style.background";
NSLog(@"color is %@",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
NSLog(@"did load finish ");
}
Run Code Online (Sandbox Code Playgroud)
//输出是-----
2013-09-03 11:56:33.187广告[3655:11303]颜色为绿色
2013-09-03 11:56:33.188广告[3655:11303]加载完成
在HTML资源中.我用
body bgcolor="#FF0000"
Run Code Online (Sandbox Code Playgroud)
在那种情况下,我的代码应该是.
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"Done loading");
NSLog(@"color1: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.bgColor"]);
}
Run Code Online (Sandbox Code Playgroud)
产量是 -
2013-09-02 16:47:53.986 WebViewTest[14674:707] color1: #FF0000
Run Code Online (Sandbox Code Playgroud)
现在,在HTML资源中,我使用
body style="background-color:#FF0000"
Run Code Online (Sandbox Code Playgroud)
在那种情况下,我的代码应该是.
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"Done loading");
NSLog(@"color1: %@", [webView stringByEvaluatingJavaScriptFromString: @"document.body.style.backgroundColor"]);
}
Run Code Online (Sandbox Code Playgroud)
产量是 -
2013-09-02 16:55:11.531 WebViewTest[14691:707] color1: rgb(255, 0, 0)
Run Code Online (Sandbox Code Playgroud)
如您所见,根据您将bgcolor应用于HTML的方式,也会改变您通过java脚本访问它的方式....
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
1314 次 |
| 最近记录: |