Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 4ca2068

Browse files
committed
Merge pull request #249 from NativeScript/icon-fonts
Add "Icon Fonts" article
2 parents e9ea66e + b1597f9 commit 4ca2068

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
238 KB
Loading

img/modules/icon-fonts/icomoon.png

67.2 KB
Loading
178 KB
Loading

ui/icon-fonts.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Icon Fonts
3+
description: Learn how to use icon fonts to display icons instead of using images.
4+
position: 13
5+
slug: icon-fonts
6+
previous_url: /icon-fonts
7+
---
8+
9+
# Icon Fonts
10+
While bitmap images are great, they present challenges in designing mobile applications. Images increase the size of the application if they are embedded in it. If not, they require additional http requests in order to be fetched. Images consume memory. Furthermore, bitmap images do not scale well. If scaled up, they will lose quality. If scaled down, they will waste space. On the other hand, fonts scale well, do not require additional http requests for each glyph and do not increase memory usage significantly. Icon fonts are fonts that contain icons instead of alphabet characters and can be used insteas of images in mobile applications.
11+
12+
# Using Icon Fonts in NativeScript
13+
Choose or generate an icon font that best matches your needs. Two popular icon fonts are [IcoMoon](https://icomoon.io/) and [Font Awesome](https://fortawesome.github.io/Font-Awesome/). Once you have downloaded the icon font to your machine, locate the [TrueType](https://en.wikipedia.org/wiki/TrueType) font file with extension **.ttf**. In your NativeScript application **app** folder create a folder called **fonts** and place the **.ttf** there. Follow the instructions on the icon font webpage to determine the hex codes of each font glyph, i.e. icon. Add a **Label** component to your NativeScript app and bind the Label's **text** property to a one-letter string generated from the character code of the icon you want to show, i.e. `String.fromCharCode(0xe903)`. Do not forget to set the Label's **font-family** to the name of your font either through CSS, XML or code-behind.
14+
15+
## Icon Font
16+
![icomoon]({{site.baseurl}}/img/modules/icon-fonts/icomoon.png "Icomoon")
17+
18+
## Fonts Folder
19+
![icon-fonts]({{site.baseurl}}/img/modules/icon-fonts/fonts-folder.png "Fonts Folder")
20+
21+
## app.css
22+
``` CSS
23+
.icon {
24+
font-family: 'icomoon';
25+
font-size: 48;
26+
}
27+
```
28+
29+
## main-page.xml
30+
``` XML
31+
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded">
32+
<ListView items="{{ glyphs }}">
33+
<ListView.itemTemplate>
34+
<StackLayout orientation="horizontal">
35+
<Label text="{{ icon }}" class="icon"/>
36+
<Label text="{{ code }}" />
37+
</StackLayout>
38+
</ListView.itemTemplate>
39+
</ListView>
40+
</Page>
41+
```
42+
43+
## main-page.js/ts
44+
``` JavaScript
45+
"use strict";
46+
var observable = require("data/observable");
47+
function pageLoaded(args) {
48+
var page = args.object;
49+
var viewModel = new observable.Observable();
50+
var glyphs = new Array();
51+
var charCode = 0xe903;
52+
for (; charCode <= 0xeaea; charCode++) {
53+
var glyph = new observable.Observable();
54+
glyph.set("icon", String.fromCharCode(charCode));
55+
glyph.set("code", charCode.toString(16));
56+
glyphs.push(glyph);
57+
}
58+
viewModel.set("glyphs", glyphs);
59+
page.bindingContext = viewModel;
60+
}
61+
exports.pageLoaded = pageLoaded;
62+
//# sourceMappingURL=main-page.js.map
63+
```
64+
``` TypeScript
65+
import observable = require("data/observable");
66+
import pages = require("ui/page");
67+
68+
export function pageLoaded(args: observable.EventData) {
69+
var page = <pages.Page>args.object;
70+
var viewModel = new observable.Observable();
71+
var glyphs = new Array<observable.Observable>();
72+
var charCode = 0xe903;
73+
for(; charCode <= 0xeaea; charCode++){
74+
var glyph = new observable.Observable();
75+
glyph.set("icon", String.fromCharCode(charCode));
76+
glyph.set("code", charCode.toString(16));
77+
glyphs.push(glyph);
78+
}
79+
viewModel.set("glyphs", glyphs)
80+
page.bindingContext = viewModel;
81+
}
82+
```
83+
84+
## Sample App
85+
![sample-app]({{site.baseurl}}/img/modules/icon-fonts/sample-app.png "Sample App")
86+
87+
[Sample Application](https://github.com/NativeScript/icon-fonts)

0 commit comments

Comments
 (0)