Introducing React ReExt – Sencha Ext JS Components in React! LEARN MORE

Training Tip: Using Custom Fonts with Sencha Architect

July 1, 2014 117 Views
Show

Back in the days when websites were restricted to typical fonts such as Arial, Georgia, Times and so on, fonts were delivered from the user’s machine. Fortunately today, because of HTML5 and CSS3, browsers support embedded web fonts.

In this month’s training tip, I will explain how to use custom fonts with Sencha Architect. For this tutorial, I will use one of the Sencha Touch starter apps, with the default theme. However, you can also use these tips for other Sencha Touch or Ext JS apps.

Incorporating a custom font from a font service

If you haven’t done so, drag the Default theme into your project (I used one of the Sencha Touch starter apps). Apply the Default theme, to create a custom theme extension.

  1. Select the MyDefaultTheme, and click on the scss + button, to add a new Sass snippet.
  2. Click on the right arrow button of the Scss Resource to enter the Sass code snippet properties.
  3. Set the compile order to beforeVariables
  4. Note: One thing you should know about working with custom fonts is that font imports must always be at the very top of your stylesheet. If you don’t change the compile order of the Scss resource, this snippet will be inserted after all the Sencha theme styles are inserted. That would cause your custom fonts to not be visible.

  5. A font service (or online font provider) is a directory of free hosted web fonts. This is nice, because with a font service, you don’t need to host the fonts locally, and therefore you don’t have to deal with font copyrights. There are a couple of popular font services, such as Google Web Font or Typekit.
  6. Let’s use Google Web Font and you can choose a font you like.

    Once, you have found the font you want, click the Add to Collection button.

    Next, click the “Use” tab. Scroll down to paragraph #3, and click the @import tab. Then, copy the code it displays.

  7. Open Architect, in the Code editor paste the font code, for example:
  8. @import
    url(http://fonts.googleapis.com/css?family=Exo&subset=latin,latin-ext);

  9. Select the MyDefaultTheme and click on the Theme tab in the config inspector.
  10. Here, filter for font. In Ext.Class, you can set the font family. Set it to the following value (depending on the font you choose):
  11. ‘Exo’, sans-serif;

Now go back to the Design view, (if you don’t see anything, hit the “Refresh” button) and preview your new font.

Incorporating a custom @font-face (local) font

But what if you want to use your own font? Or, maybe your company has its own font you want to use. Before we get into that, let me explain a little more about local fonts.

@font-face is a CSS technique used to integrate custom web fonts. With system fonts, it picks the font if available in your OS, @font-face downloads the font from the Internet. Unfortunately, the major browsers have not standardized on one web font solution. Therefore, you have to embed multiple webfont extensions into your stylesheet.

You can get these packages of fonts online. For example, http://www.fontsquirrel.com is a website where you can download fonts, 100% free for commercial use.

  1. Select the MyDefaultTheme, and click on the scss + button, to add a new Sass snippet.
  2. Click on the right arrow button of the Scss Resource to enter the Sass code snippet properties.
  3. Set the compile order to beforeVariables
  4. Now let’s download a nice font-face kit from font-squirel.com to your hard drive, for example:
    http://www.fontsquirrel.com/fonts/exo-2
  5. Click on the Webfont kit tab, make sure the following extensions are included in the package: ttf, eot, woff, svg, and hit the blue Download @Font-face Kit button.

  6. Extract the zip file, and open one of the regular font folders. Check out the .css stylesheet that’s included, and copy the import lines of code.
  7. Go back to Sencha Architect, in the Code editor of the MyDefaultTheme, and paste the import. For example:
  8. @font-face {
    font-family: ‘exo_2.0regular’;
    src: url(‘Exo2.0-Regular-webfont.eot’);
    src: url(‘Exo2.0-Regular-webfont.eot?#iefix’) format(’embedded-opentype’),
    url(‘Exo2.0-Regular-webfont.woff’) format(‘woff’),
    url(‘Exo2.0-Regular-webfont.ttf’) format(‘truetype’),
    url(‘Exo2.0-Regular-webfont.svg#exo_2.0regular’) format(‘svg’);
    font-weight: normal;
    font-style: normal;
    }

  9. Now save your Architect project, and copy the 4 font extensions over to your project resources folder in your file system; if you want, you may create a subfolder fonts here.
  10. Since my fonts are located in resources/fonts/, I need to fix the path in my Architect snippet. Sencha Architect expects the fonts to be located in the /theme/ folder. I also renamed the font-family name:
  11. @import url(http://fonts.googleapis.com/css?family=Exo&subset=latin,latin-ext);

    @font-face {
    font-family: ‘Exo2’;
    src: url(‘../../resources/fonts/Exo2.0-Regular-webfont.eot’);
    src: url(‘../../resources/fonts/Exo2.0-Regular-webfont.eot?#iefix’) format(’embedded-opentype’),
    url(‘../../resources/fonts/Exo2.0-Regular-webfont.woff’) format(‘woff’),
    url(‘../../resources/fonts/Exo2.0-Regular-webfont.ttf’) format(‘truetype’),
    url(‘../../resources/fonts/Exo2.0-Regular-webfont.svg#exo_2.0regular’) format(‘svg’);
    font-weight: normal;
    font-style: normal;
    }

  12. Select the MyDefaultTheme, and click on the Theme tab.
  13. Here, filter for font. In Ext.Class, you can set the font family. Set it to the following value: (depending on the font you choose):
  14. ‘Exo2’;

  15. Compile your project, and test the result in your browser. You will see the new font.

Looking for more help with theming for Sencha Touch? Sign up for a training class. In July, we will offer an advanced Sencha Touch online theming course.

Show
Start building with Ext JS today

Build 10x web apps faster with 140+ pre-build components and tools.

Latest Content
Discover the Top 07 Architecture Patterns used in Modern Enterprise Software Development
Discover the Top 07 Architecture Patterns used in Modern Enterprise Software Development

Developing software without an architecture pattern may have been an option back then. However, that’s…

JavaScript Design Patterns: A Hands-On Guide with Real-world Examples
JavaScript Design Patterns: A Hands-On Guide with Real-world Examples

As a web developer, you know how popular JavaScript is in the web app development…

Virtual JS Days 2024のハイライト
Virtual JS Days 2024のハイライト

2024年2月20日~22日、第3回目となる「Virtual JavaScript Days」が開催されました。JavaScript の幅広いトピックを採り上げた数多くのセッションを実施。その内容は、Senchaの最新製品、ReExt、Rapid Ext JSまで多岐にわたり、JavaScriptの最新のサンプルも含まれます。 このカンファレンスでは多くのトピックをカバーしています。Senchaでセールスエンジニアを務めるMarc Gusmano氏は、注目すべきセッションを主催しました。Marc は Sencha の最新製品「ReExt」について、詳細なプレゼンテーションを実施。その機能とメリットを、参加者に理解してもらうべく詳細に説明しました。 カンファレンスは、Senchaのジェネラルマネージャを務めるStephen Strake氏によるキーノートでスタートしました。キーノートでは、会社の将来のビジョンについての洞察を共有しています。世界中から JavaScript 開発者、エンジニア、愛好家が集まるとてもエキサイティングなイベントとなりました。これは、JavaScript…

See More