简体   繁体   English

如何在Phonegap中更改字体系列

[英]How can i change the font family in phonegap

I want to change font family in a phonegap project. 我想在phonegap项目中更改字体系列。

Means that user be able to change font family in the setting by select the arbitrary font. 意味着用户可以通过选择任意字体来更改设置中的字体系列。

How can i do it? 我该怎么做?

Thanks in Advance 提前致谢


Edit: Seems it doesn't work.this is my jquery code: 编辑:似乎不起作用。这是我的jQuery代码:

 $("#font-selector").change(function(){
       $(".size").css("font-family",$("#font-selector").val()); 

   });

And html code is: 和html代码是:

<select name="changeFont" id="font-selector" onchange='$("body").attr("data-font", $("#font-selector").val())'>
<option value="my font">my font</option>
<option value="my font">my font</option>

And css code is: CSS代码是:

@font-face {
font-family: 'my font';
src: url('my font.eot');
src: url('my font.eot?#iefix') format('embedded-opentype'),
     url('my font.woff2') format('woff2'),
     url('my font.woff') format('woff'),
     url('my font.ttf') format('truetype'),
     url('my font.svg#my font') format('svg');
font-weight: normal;
font-style: normal;

Where is the problem? 问题出在哪儿?

You can use this plugin to get a list of all fonts of your device: 您可以使用此插件来获取设备所有字体的列表:

cordova-plugin-fonts 科尔多瓦-插件的字体

Then you can show to the user a list, let them select one and after that you change the css of your site by javascript. 然后,您可以向用户显示一个列表,让他们选择一个列表,然后用javascript更改网站的CSS。

I'm not sure, but it could be, that you have to rerender the page after changing the font. 我不确定,但是可能是,更改字体后必须重新渲染页面。

Seems something you could easily do with javascript/css. 似乎可以使用javascript / css轻松完成。

Just ship your fonts in the app, to make sure they are available on any platform, and add a listener to your select which changes a css class to the body. 只需将您的字体运送到应用程序中,以确保它们可在任何平台上使用,然后将侦听器添加到您的选择中即可将CSS类更改为正文。

This is the html code if you are using jQuery or Zepto syntax. 如果您使用的是jQuery或Zepto语法,则这是html代码。

<select name="changeFont" id="font-selector"  onchange='$("body").attr("data-font", $("#font-selector").val())'>
    <option value="default">Default</option>
    <option value="arial">Arial</option>
    <option value="comic-sans">Comic Sans</option>
</select>

then in css 然后在CSS

.mytext {
  font-family: helvetica, sans-serif;
}

[data-font=arial] .mytext {
  font-family: arial, sans-serif;
}

[data-font=comic-sans] .mytext {
  font-family: "comic sans", sans-serif;
}

For this to work, just make sure that you have correctly added your fonts. 为此,只需确保已正确添加字体。 To make this right you could use FontSquirrel webfont generator to convert any font to a web font and generate the needed css for you. 为此,您可以使用FontSquirrel Webfont生成器将任何字体转换为Web字体并为您生成所需的CSS。

I should add that I did not test this, so let me know if you find it's not working. 我还要补充一点,我没有对此进行测试,所以如果您发现它不起作用,请告诉我。


Or, if your app is designed to work only when the user has internet connection then there's an even quicker way to add a lot of fonts. 或者,如果您的应用程序设计为仅在用户具有互联网连接时才可以运行,那么有一种添加许多字体的更快方法。 There's a jQuery plugin that will do exactly that. 有一个jQuery插件可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM