Android PhoneGap中的自定义字体

21 android custom-font cordova

我试图为我的应用程序制作自定义字体.为此,我在我的html文件中编写了这段代码:

<style type="text/css" media="screen">
        @font-face {
            font-family: centurySchoolbook;
            src: url(/fonts/arial.ttf);
        }
       body {
           font-family: centurySchoolbook;
           font-size:30px;
       }
</style>
Run Code Online (Sandbox Code Playgroud)

在我的Html正文中:

<body onload="init();">
<h>Custom Fonts</h>
    <div>This is for sample</div>

</body>
Run Code Online (Sandbox Code Playgroud)

但是,这些样式不适用于我的html体..任何帮助解决这个问题.. ??

Lit*_*ttm 32

我在完成以下步骤后才开始工作:

- 将CSS放在一个文件中,例如my_css.css:

@font-face {
    font-family: "customfont";
    src: url("./fonts/arial.ttf") format("opentype");   
        /* Make sure you defined the correct path, which is related to
            the location of your file `my_css.css` */ 
    }
    body {
        font-family: "customfont";
        font-size:30px;
    }
Run Code Online (Sandbox Code Playgroud)

- my_css.css在HTML中引用您的CSS文件:

<link rel="stylesheet" href="./path_to_your_css/my_css.css" />


注意路径的定义!

例如,假设您具有以下目录结构:

  • 万维网

    • your_html.html

    • CSS

      • my_css.css
    • 字体

      • arial.ttf

然后,你会有:

@font-face {
    font-family: "customfont";
    src: url("../fonts/arial.ttf") format("opentype");   
        /* Make sure you defined the correct path, which is related to
            the location of your file `my_css.css` */ 
    }
    body {
        font-family: "customfont";
        font-size:30px;
    }
Run Code Online (Sandbox Code Playgroud)

和:

<link rel="stylesheet" href="./css/my_css.css" />


注意:如果你使用jQuery Mobile,解决方案可能无效(jQuery Mobile将"干扰"css ...).

因此,您可以尝试使用style属性强加您的样式.

例如:

<body>
    <h>Custom Fonts</h>
    <div style="font-family: 'customfont';">This is for sample</div>
</body>
Run Code Online (Sandbox Code Playgroud)

一个缺点是似乎style无法应用body......

因此,如果您想将字体应用于整个页面,您可以尝试这样的事情:

<body>

    <!-- ADD ADDITIONAL DIV WHICH WILL IMPOSE STYLE -->
    <div style="font-family: 'customfont';">

        <h>Custom Fonts</h>
        <div >This is for sample</div>

    </div>

</body>
Run Code Online (Sandbox Code Playgroud)

希望这也适合你.让我知道你的结果.

  • 谢谢你的帖子,我尝试了同样正确的路径..但​​是,仍然没有变化. (3认同)