如何在 WebView 中使用自定义字体
现在我想显示一些unicode字符并且我使用了标签: 这里有东西
。但是WebView
似乎找不到Arial 字体,因为我只能看到 UFO 字符。我是否必须将 arial.ttf 复制到 某处或者如何在 WebView
中使用此 TrueType 字体?谢谢。
Now I want to display some unicode characters and I have used tag: <font face="
. But it seems that
Arial">something here</font>WebView
can not find the Arial
font because I can only see UFO-characters. Do I have to copy arial.ttf to
somewhere or how can I use this TrueType font with WebView
? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
loadData
也不适合我,所以我在 src 路径中使用file:///android_asset
。它与
loadDataWithBaseURL
一起使用!对于此示例,我将 CSS 更改为:
然后使用资产路径作为基本 url:
loadData
didn't work for me either, so I usedfile:///android_asset
in the src path.It worked with
loadDataWithBaseURL
!For this example I changed the CSS to:
Then use the assets path as the base url:
显然,您可以为
WebView
使用自定义字体,正如上面@raychung 所建议的那样。但这不适用于 2.1(此处报告了错误)。这应该适用于 1.5、1.6 和 2.2。您可以将自定义字体 TTF 文件放入
/assets
文件夹中,然后在 CSS 文件中放入:您现在可以在 HTML 文件中引用此字体样式。
Apparently, you can use a custom font for
WebView
, as @raychung above suggested. But this won't work for 2.1 (Bug is reported here). This should work for 1.5, 1.6 and 2.2.You can put your custom font TTF file in your
/assets
folder, then in your CSS file you can put in:You can now reference this font style in your HTML file.
您可以在首次启动应用程序时将字体文件从资产复制到文件夹中,然后将其引用为:
You can get it to work on all versions by copying the font file from your assets to your files folder on the first launch of the app, and then reference it as:
这对我有用。
It works for me.
我使用下面的代码用于带有波斯字体的 RTL 方向,希望有人使用此代码或有人建议我最好的方法。谢谢
I used below code for rtl direction with persian font, hope someone used this code or someone suggest me best way. thanks
它对我不起作用,我必须链接网络服务器上的字体,而不是使用对本地文件的引用:
It didn't worked for me, I have had to link the font on my web server instead of using the reference to a local file:
这在我测试过的所有设备上都非常适合我。
将您的字体文件放在 /src/main/assets/fonts 中,然后使用以下方法:
如下
希望
对某人有所帮助!
如果您想保留 html 代码中的字体大小,请删除
请记住 1rem 相当于大约 14sp
This works great for me on all the devices I've tested it on.
Place your font files in /src/main/assets/fonts, then use this method:
as follows
where
Hope it helps someone!
If you want to keep the font size from your html code remove
Have in mind that 1rem is equivalent to about 14sp
正如 Felipe Martinez Carreño 所说,您可以从资产中进行复制,并且它会起作用。
您可以参考此了解更多信息细节
As Felipe Martinez Carreño said you can copy from assets and it will work.
You can refer this for more details
基本思想是网页应该能够访问字体文件。这就是为什么当 HTML 文件和字体文件都位于 asset 文件夹中并且 HTML 文件从那里加载时,建议的原始解决方案起作用。
它不是 Android 功能,而是 CSS,因此应该适用于任何 Android SDK。
如果字体文件确实存在并且样式中给出了正确的路径,则从文件文件夹加载字体也应该有效。但这种方法比较混乱,需要编写代码来复制文件,然后编写代码来找出文件的位置。
我很高兴只需将 HTML 内容和字体文件放在资源中,然后从那里加载。
The basic idea is that the web page should have access to the font file. This is why the suggested original solution works when both the HTML file and the font file are in the assets folder, and the HTML file is loaded from there.
It isn't an Android feature, it's CSS, so should work with any Android SDK.
Loading the font from the files folder should work as well, if the font file is really there and the correct path to it is given in the style. But tho approach is messier, one needs to write code to copy the file, then code to figure out the location of the file.
I am quite happy to simply have the HTML content and the font file in the assets, and loading from there.
使用CSS
use css