webview的自定义字体 [英] Custom font for webview

查看:26
本文介绍了webview的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 Web 视图建立一个新的自定义字体(otf 文件).

I am trying to establish a new custom font (otf file) for my web view.

我将 anbaaarabic_bold.otf 放在资产文件中.

I put anbaaarabic_bold.otf in assets file.

我是这样做的:

String head = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ getActivity().getFilesDir().getAbsolutePath()+ "/anbaaarabic_bold.otf');}body {font-family: 'verdana';}</style></head>";
String htmlData= "<html>"+head+"<body style="font-family: verdana">"+body+"</body></html>" ;

mBodyArticle.loadDataWithBaseURL("http://nada", htmlData,
                    "text/html", "utf-8", "");

body : 包含我的 html 代码.

body : contain my html code.

我不知道为什么字体不适用于我的 web 视图.body html 字体不会改变.

I don't know why the font is not working for my webview. body html font doesn't change.

推荐答案

有一个错误报告 似乎表明自定义字体不适用于某些语言(已提及泰语、希伯来语、波斯语和阿拉伯语).谷歌的任何人都没有证实这一点,但有很多人报告了同样的问题.

There is a bug report for Android that seems to suggest that custom fonts don't work for certain languages (Thai, Hebrew, Farsi, and Arabic have been mentioned). This hasn't been confirmed by anyone from Google, but there are a number of people reporting the same problem.

建议的解决方案之一(至少有一位用户确认该解决方案有效)是将字体从 otf 转换为 svg.有许多在线工具可以为您执行此操作(Everything Fonts 就是一个示例),但作为受版权保护的字体,您可能会遇到问题 - 请确保您的字体许可允许此类转换.

One of the suggested solutions (which at least one user confirmed as working) was to convert the font from otf to svg. There are a number of online tools that will do this for you (Everything Fonts is one example), but you may have issues with this being a copyrighted font - make sure your font license permits such conversions.

此外,当您转换字体时,请在文本编辑器中打开它并检查 svg 元素是否已命名空间.它应该看起来像这样:

Also, when you have converted the font, open it up in a text editor and check whether the svg element is namespaced. It should look something like this:

<svg xmlns="http://www.w3.org/2000/svg">

如果只是 没有命名空间,你可能需要手动添加 xmlns 属性.显然,没有命名空间的 svg 字体也不起作用.

If it is just <svg> without the namespace, you may need to add the xmlns attribute manually. Apparently, svg fonts without the namespace don't work either.

这篇关于webview的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆