| 下載活動 | 特拉維斯CI | Snyk |
|---|---|---|
一個列舉安裝在本地設備上的字體,並提供默認字體的名稱的Cordova插件。
該插件定義了一個全局Fonts對象,該對象可訪問對設備上安裝的字體。在deviceready事件觸發後,可以從navigator對象獲得Fonts對象。
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.Fonts);
}
從命令行:
cordova plugin add cordova-plugin-fonts
config.xml用於電話蓋構建:
<gap:plugin name="cordova-plugin-fonts" source="npm" />
這些命令將從NPM安裝插件。您可以在此處的NPM上找到此插件,或通過搜索像這樣的NPM註冊表中的ecosystem:cordova 。
Fonts對象提供了一種列舉設備上安裝的字體列表的方法。
當前,該插件提供了兩種方法, GetFontList和GetDefaultFont 。
參數:
Firefox OS怪癖
Firefox OS不提供API來訪問設備上的字體。字體插件當前返回與fonts.mk文件相對應的列表(https://github.com/mozilla-b2g/moztt/mozt/moztt/blob/master/fonts.mk ),但是它是一個硬編碼的列表,並且不能在任何特定的版本上都正確列出。
if (navigator.Fonts) {
console.log("Fonts object in navigator");
navigator.Fonts.getFontList(
function (fontList) {
if (fontlist) {
for (var i = 0; i < fontlist.length; i++) {
console.log("Font: " + fontlist[i]);
}
}
},
function (error) {
console.log("FontList error: " + error);
}
);
} else {
console.log("Plugin error: Fonts plugin not found (is it installed?)");
}
參數:
Firefox OS怪癖
Firefox OS不提供API來訪問設備上的字體。字體插件當前返回默認字體“ Fira sans常規”的硬編碼字符串。有關更多信息,請參見https://www.mozilla.org/en-us/styleguide/products/firefox-os/typeface/。
if (navigator.Fonts) {
console.log("Fonts object in navigator");
navigator.Fonts.getDefaultFont(
function (defaultFont) {
if (defaultFont) {
console.log("Default Font: " + defaultFont);
}
},
function (error) {
console.log("DefaultFont error: " + error);
}
);
} else {
console.log("Plugin error: Fonts plugin not found (is it installed?)");
}
(這僅適用於開發 /調試插件本身的開發人員)
Cordova-Fonts插件使用Cordova-Plugin-Test-Framework進行單元測試。完成以下內容以通過插件單元測試運行:
使用您現有的Cordova應用程序,或創建一個新應用程序。您還可以在https://github.com/eb1/test-fonts上使用已經為此設置的測試項目(只需使用在此處列出的說明而不是下面的說明)。
添加Cordova-Fonts插件和測試 /測試框架插件:
cordova plugin add https://github.com/adapt-it/cordova-fonts.git
cordova plugin add https://github.com/adapt-it/cordova-fonts.git#:/tests
cordova plugin add https://github.com/apache/cordova-plugin-test-framework.git
cdvtests/index.html <content src="cdvtests/index.html" />更改Cordova應用config.xml中的啟動頁面。
在模擬器或設備上構建並運行應用程序。