Android 系统字体大小被设置为“中号”,造成 APP 内 WebView 布局混乱,系统浏览器布局正常。
表现为 html 页面的 css 样式中 font-size 值,不等于实际渲染的值,如:
1 2 3 4 5 6 |
<html style="font-size: 100px;"> <script> console.log(window.getComputedStyle(document.documentElement).fontSize); // output: 104px </script> </html> |
可通过设置 WebView 的 TextZoom 属性解决:
1 2 |
WebSettings settings = webView.getSettings(); settings.setTextZoom(100); |
IOS 平台可设置(未经验证):
1 2 3 |
body { -webkit-text-size-adjust: none !important; } |
微信平台可设置(未经验证):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
(function() { if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") { handleFontSize(); } else { if (document.addEventListener) { document.addEventListener("WeixinJSBridgeReady", handleFontSize, false); } else if (document.attachEvent) { document.attachEvent("WeixinJSBridgeReady", handleFontSize); document.attachEvent("onWeixinJSBridgeReady", handleFontSize); } } function handleFontSize() { // 设置网页字体为默认大小 WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize' : 0 }); // 重写设置网页字体大小的事件 WeixinJSBridge.on('menu:setfont', function() { WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize' : 0 }); }); } })(); |