iOS-WKWebView 加载HTML字符自适应文字和图片

2022-11-10  本文已影响0人  见路_不走

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];

NSString *jSString =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript *script = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

NSString *htmlString = [NSString stringWithFormat:@"<html> \n""<head> \n""<style type=\"text/css\"> \n""body {font-size:15px;}\n""</style> \n""</head> \n""<body>""<script type='text/javascript'>""window.onload = function(){\n""var $img = document.getElementsByTagName('img');\n""var maxWidth = %f;\n""for(var k in $img){\n""if($img[k].width> maxWidth){\n"" $img[k].style.width = maxWidth;\n""}\n""$img[k].style.height ='auto'\n""}\n""}""</script>%@""</body>""</html>", _maxWidth,string];

    [self.webView loadHTMLString:htmlString baseURL:nil];

上一篇 下一篇

猜你喜欢

热点阅读