【发布时间】:2019-04-15 03:09:02
【问题描述】:
我需要实现可以离线渲染 HTML 的 WebView。 HTML 中可能使用了多种字体,我将它们全部下载到使用context.getFilesDir() 获得的应用程序文件目录中。
我的目标:将这些字体文件“输入”到 WebView 中,以使呈现的 HTML 看起来正确。
到目前为止我尝试了什么。
通过以下方式将 HTML 绑定到 WebView:
baseUrl = "file:///data/user/0/com.myapp/files/fonts"; // this is for convenience, I don't hardcode like this in code
webView.loadDataWithBaseURL(baseUrl, html, "text/html; charset=utf-8", "UTF-8", null);
指定文件夹包含所有下载的字体。
我还将@font-face 配置添加到 HTML 的标题中,如下所示:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style id="fonts">@font-face { font-family: Guttman Stam; src: url(/Guttman Stam.ttf); }</style>
</head>
<body>
<p><span style="background-color:#FFFFFF;font-family:Guttman Stam;font-size:26pt;">text</span></p>
</body>
</html>
我确定文件 /data/user/0/com.myapp/files/fonts/Guttman Stam.ttf 存在。
不幸的是,它不起作用。我还尝试在@font-face 配置中提供完整路径,同时使用null 和"" 作为WebView 的baseUrl,但没有运气。
所以,我有点迷路了。我们将不胜感激。
【问题讨论】:
标签: android html css android-webview font-face