如何为所有文本声明应用程序范围的自定义字体

时间:2017-05-19 13:30:12

标签: fonts appcelerator appcelerator-alloy

我已经将自定义字体放入我的app/resources/<platform>/fonts文件夹并在我的应用中的几个UI元素上使用它,但有没有办法在整个应用中声明这是文本的默认字体? 我会假设这可能会在tiapp.xml中完成吗?否则,我能想到实现这一目标的唯一方法是在app.tss中单独设置每个元素的字体,但这很快就会变得乏味,并且可以跳过一个元素。我确信那里必须有更好的解决方案。

2 个答案:

答案 0 :(得分:2)

TSS文件是在app宽文本/标签/按钮等上应用自定义字体的最佳位置。

您可以这样做:

<强> app.tss

"Label" :   {
    font : {
        fontFamily : 'custom_font_1'  // app->assets->fonts folder
    }
}


".custom_font" :   {
    font : {
        fontFamily : 'custom_font_file'  // app->assets->fonts folder
    }
}

<小时/> 的 ViewName.xml

<Label class="custom_font" text="Hello there!" color="black" />
<Label class="view-class" text="Hello there!" color="blue" />
<Label text="Hello there!" color="red" />

<小时/> 的 ViewName.tss

".view-class" :   {
    font : {
        fontFamily : 'another_font'  // app->assets->fonts folder
    }
}

在此示例中,我们在 3 位置定义了不同的字体。 app.tss 文件中的 2 字体,以及 ViewName.tss 文件中的 1 字体。

ViewName.xml 文件中:

  • 1st Label将使用 app.tss
  • 中的 custom_font
  • 2nd Label将使用 ViewName.hss
  • 中的 view-class
  • 3rd Label将使用 app.tss
  • 中的默认标签字体

所以,我已经向您介绍了满足每个应用程序要求的不同方法。你的具体案例是Label 3rd。

就像您在 app.tss 中使用标签一样,您可以使用任何Titanium文本类型元素,如TextField,TextArea,Button等。

请参阅此处:Global Styling & Themes in Titanium

答案 1 :(得分:0)

实际上,您需要在app.tss中为要设置样式的所有对象类型定义它。我想是唯一的方法。