从 Android“资产”访问 CSS 字体文件夹

发布于 2024-09-28 01:25:35 字数 539 浏览 3 评论 0原文

我正在尝试更改加载到为 Android 开发的浏览器应用程序中的网页字体。目前我的自定义字体托管在 URL 和我通过向页面注入 JavaScript 来更改样式表 (CSS) 字体,从而从那里访问字体。

这是当前的代码:

var css = '@font-face {font-family: "dhanika"; src: url(http://www.sample.lk/DhanikaWeb.ttf);} si {font-family: "dhanika"}';
var style = document.getElementsByTagName("style")[0];
style.appendChild(document.createTextNode(css));

该代码完美运行。但从 URL 加载字体需要一些时间。而且每次都加载字体并不好,因为可能会产生数据费用。

因此,如果我将字体放在应用程序的“资产”文件夹中,我想知道是否有一种方法可以从 JavaScript 访问我的自定义字体。很高兴有人能提供代码。

任何帮助表示赞赏!

I'm trying to change the font of web pages loaded in to a Browser app developed for Android. Currently my custom font is hosted in a URL & I access the font from there by injecting a JavaScript to the page to change the style sheet (CSS) font face.

Here is the current code:

var css = '@font-face {font-family: "dhanika"; src: url(http://www.sample.lk/DhanikaWeb.ttf);} si {font-family: "dhanika"}';
var style = document.getElementsByTagName("style")[0];
style.appendChild(document.createTextNode(css));

This code works perfectly. But it takes some time to load the font from the URL. And also it is not good to load the font each time since it may incur data charges.

Therefore I wanna know weather there is a method to access my custom font from the JavaScript, if I place my font in my app's 'Assets' folder. Glad if anyone can provide the code.

Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-10-05 01:25:35

据我对 Android 和 @font-face 的了解,Android 目前因 local() 声明而窒息。在所有浏览器中使用 @font-face 的最佳方法是使用双重声明。示例

@font-face { font-family:'fontName'; src:url('fontName.eot'); font-weight:normal; font-style:normal; }
@font-face { font-family:'fontName'; src:url(//:) format('no404'), url('fontName.woff') format('woff'), url('fontName.ttf') format('truetype'), url('fontName.svg') format('svg'); font-weight:normal; font-style:normal; }

最简单的方法是使用 FontSquirrel 的 @font-face 生成器

From what I know about Android and @font-face, Android currently chokes on the local() declaration. The best method for using @font-face with all browsers is to use double declarations. Example

@font-face { font-family:'fontName'; src:url('fontName.eot'); font-weight:normal; font-style:normal; }
@font-face { font-family:'fontName'; src:url(//:) format('no404'), url('fontName.woff') format('woff'), url('fontName.ttf') format('truetype'), url('fontName.svg') format('svg'); font-weight:normal; font-style:normal; }

The Easiest way to do this is to use FontSquirrel's @font-face generator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文