Skip to content

docs: update Icon Fonts article #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/app.css
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
@import '~nativescript-theme-core/css/core.light.css';
@import '~nativescript-theme-core/css/core.light.css';

.fa {
font-family: "Font Awesome 5 Free", "fa-regular-400";
}

.fa-brands {
font-family: "Font Awesome 5 Brands", "fa-brands-400";
}

.fa-solid {
font-family: "Font Awesome 5 Free", "fa-solid-900";
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.icon{
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.icon{
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.icon{
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
}
2 changes: 1 addition & 1 deletion app/css/core.dark.android.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/css/core.dark.ios.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/css/core.light.android.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/css/core.light.ios.css

Large diffs are not rendered by default.

Binary file added app/fonts/IcoMoon-Free.ttf
Binary file not shown.
Binary file added app/fonts/fa-brands-400.ttf
Binary file not shown.
Binary file added app/fonts/fa-regular-400.ttf
Binary file not shown.
Binary file added app/fonts/fa-solid-900.ttf
Binary file not shown.
Binary file removed app/fonts/icomoon.ttf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit } from "@angular/core";
import * as imageSource from "image-source";
import * as imageSource from "tns-core-modules/image-source";
// >> fs-read-import-code
import * as fs from "tns-core-modules/file-system";
// << fs-read-import-code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.my-label {
border-radius: 5;
color: white;
font-family: FontAwesome;
font-family: "fa-regular-400";
font-size: 24;
vertical-align: middle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.my-label {
border-radius: 5;
color: white;
font-family: FontAwesome;
font-family: "fa-regular-400";
font-size: 24;
vertical-align: middle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.my-label {
border-radius: 5;
color: white;
font-family: FontAwesome;
font-family: "fa-regular-400";
font-size: 24;
vertical-align: middle;
}
93 changes: 22 additions & 71 deletions app/ng-ui-widgets-category/icon-fonts/basics/basic.component.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,22 @@
<GridLayout class="p-15" rows="*" columns="*" sdkExampleTitle sdkToggleNavButton>
<!-- >> icon-font-html-def -->
<ScrollView>
<GridLayout rows="auto auto auto auto auto auto auto" columns="">
<StackLayout class="border_style" row="0">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe903;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e903" class="icon"></Label>
</StackLayout>
<StackLayout class="border_style" row="1">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe908;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e908" class="icon"></Label>
</StackLayout>
<StackLayout class="border_style" row="2">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe90b;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e90b" class="icon"></Label>
</StackLayout>
<StackLayout class="border_style" row="3">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe90c;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e90c" class="icon"></Label>
</StackLayout>
<StackLayout class="border_style" row="4">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe914;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e914" class="icon"></Label>
</StackLayout>
<StackLayout class="border_style" row="5">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe915;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e915" class="icon"></Label>
</StackLayout>
<StackLayout class="border_style" row="6">
<Label color="#66ccff" class="m-5 icon" fontSize="34" textWrap="true">
<FormattedString>
<Span text="Icon code: "></Span>
<Span text="&#xe916;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>
<Label color="#FF6666" class="m-5" fontSize="18" textWrap="true" text="Icon code: e916" class="icon"></Label>
</StackLayout>
</GridLayout>
</ScrollView>
<!-- << icon-font-html-def -->
</GridLayout>

<StackLayout class="m-16">
<Label text="Icon Fonts demonstration" class="h3"></Label>

<!-- >> icon-font-xml-def -->
<!-- Using fa-regular-400.ttf -->
<Label text="&#xf019;" class="far"></Label>

<!-- Using fa-brands-400.ttf -->
<Label text="&#xf39e;" class="fab"></Label>

<!-- Using fa-solid-900.ttf with FormattedString -->
<Label class="fas" textWrap="true">
<FormattedString>
<Span text="&#xf053;" fontAttributes="Bold"></Span>
</FormattedString>
</Label>

<!-- Using IcoMoon-Free.ttf -->
<Label text="&#xe904;" class="ico"></Label>
<!-- << icon-font-xml-def -->
</StackLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ import { Component } from "@angular/core";
styleUrls: ["./basic.css"],
})

export class BasicIconFontsExampleComponent {

constructor() {
}
}
export class BasicIconFontsExampleComponent { }
22 changes: 20 additions & 2 deletions app/ng-ui-widgets-category/icon-fonts/basics/basic.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
/* >> icon-font-css-class-def */
.icon{
font-family: icomoon;
.far {
font-family: "Font Awesome 5 Free", "fa-regular-400";
}

.fab {
font-family: "Font Awesome 5 Brands", "fa-brands-400";
}

.fas {
font-family: "Font Awesome 5 Free", "fa-solid-900";
}

.ico {
font-family: "IcoMoon-Free";
}
/* << icon-font-css-class-def */
.border_style{
border-width: 5;
border-color:black;
padding: 5;
margin-bottom: 2;
}

Label {
font-size: 18;
color: #66ccff;
margin: 8;
}
12 changes: 12 additions & 0 deletions app/ng-ui-widgets-category/icon-fonts/code-behind/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ The example shows, how to use setup the `font-family` property via CSS and how t
<snippet id='icon-font-code-behind-html'/>
<snippet id='icon-font-code-behind-code'/>
<snippet id='icon-font-code-behind-css'/>

And the result is:

![sample-app](../img/modules/icon-fonts/sample-app.png "Sample App")

Example for icon font glyphs:

![IcoMoon](../img/modules/icon-fonts/icomoon.png "IcoMoon-Free")

Example for icon font folder location:

![icon-fonts](../img/modules/icon-fonts/fonts-folder.png "Fonts Folder")
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<GridLayout class="p-15" rows="*" columns="*" sdkExampleTitle sdkToggleNavButton>
<!-- >> icon-font-code-behind-html -->
<ListView class="icon list-group" [items]="glyphs">
<ListView [items]="glyphs">
<ng-template let-item="item">
<GridLayout class="list-group-item active" rows="* *">
<Label color="#FF6666" class="m-5 icon" fontSize="34" row="0" textWrap="true" text="{{'Icon: ' + item.icon + ' ' }}"></Label>
<Label color="#66ccff" class="m-5" fontSize="18" row="1" textWrap="true" text="{{'Icon code: ' + item.code }}"></Label>
</GridLayout>
<StackLayout orientation="horizontal">
<Label [text]="item.icon" class="ico"></Label>
<Label [text]="item.code" class="m-l-8"></Label>
</StackLayout>
</ng-template>
</ListView>
<!-- << icon-font-code-behind-html -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { Component } from "@angular/core";
})

export class CodeBehindFontsExampleComponent {
public glyphs = [];
glyphs = [];

constructor() {
for (let charCode = 0xe903; charCode <= 0xeaea; charCode++) {
let glyph = {
Expand Down
26 changes: 24 additions & 2 deletions app/ng-ui-widgets-category/icon-fonts/code-behind/codebehind.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
/* >> icon-font-code-behind-css */
.icon{
font-family: icomoon;
/*
File name: IcoMoon-Free.ttf
Font name: IcoMoon-Free
*/
.ico {
font-family: 'IcoMoon-Free';
font-size: 48;
}

/* only for reference, is NOT working in this example (due to wrong glyph codes)
.fa {
font-family: "Font Awesome 5 Free", "fa-regular-400";
font-size: 48;
}
*/
/* << icon-font-code-behind-css */

Label.icon {
font-size: 34;
color: #FF6666;
}

Label {
font-size: 18;
color: #66ccff;
}
8 changes: 0 additions & 8 deletions app/ng-ui-widgets-category/icon-fonts/end.md
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@

**API Reference for the** [HtmlView Class](http://docs.nativescript.org/api-reference/modules/_ui_html_view_.html)

**Native Component**

| Android | iOS |
|:-----------------------|:---------|
| [android.widget.TextView](http://developer.android.com/reference/android/widget/TextView.html) | [UILabel](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/) |
Binary file removed app/ng-ui-widgets-category/icon-fonts/image.png
Binary file not shown.
56 changes: 46 additions & 10 deletions app/ng-ui-widgets-category/icon-fonts/overview.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@

# Icon Fonts
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 contain icons instead of alphabet characters and can be used instead of images in mobile applications.
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 to be fetched. Images consume memory. Furthermore, bitmap images do not scale well. If scaled up, they lose quality. If scaled down, they 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 contain icons instead of alphabet characters and can be used instead of images in mobile applications.

## Using Icon Fonts in NativeScript
1. 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/).

1. Choose or generate an icon font that best matches your needs. Two popular icon fonts are [IcoMoon](https://icomoon.io/) and [Font Awesome](https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself).
2. Once you have downloaded the icon font to your machine, locate the [TrueType](https://en.wikipedia.org/wiki/TrueType) font file with extension **.ttf**.
3. In your NativeScript application **app** folder, create a folder called **fonts** and place the **.ttf** there.
3. In your root application folder (This is the **app** folder for NativeScript Core, and the **src** folder for Angular 6+), create a folder called **fonts** and place the **.ttf** there.
4. 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. Remember: Android needs the actual filename (without extension) whereas iOS needs the font name. To be cross-platform-ready, you can provide both.
> **Note:** While this documentation article is focused on icon fonts, the above workflow is a hundred percent applicable for both **text fonts** and **icon fonts** (except that with text fonts step 4 as they don't include icons but only plain text).

## Platform Specific Font Recognition

There is a conceptual difference in how **.ttf** fonts are recognized on iOS and Android. On Android, the font is recognized by its **file name** while on iOS it is recognized by its **font name**. This means that fonts that are created with a font name which is different from the file name has to be registered with both names in your CSS rule.

```CSS
.fa-brands {
font-family: "Font Awesome 5 Brands", "fa-brands-400";
}
```

In the above example, the `fa-brands-400.ttf` (as downloaded from the FontAwesome site) has a font name `Font Awesome 5 Brands`. With the above CSS, the font is recognized on both iOS (by the font name `Font Awesome 5 Brands`) and Android (by the file name `fa-brands-400`).

> **Note:** There are specific scenarios where the creators of the fonts might have released two differently named `ttf` files but with the same **font** name (see the example below).

|file name | font name |
|-----------|---------------|
|**fa-solid-900.ttf** | Font Awesome 5 Free
|**fa-regular-400.ttf** | Font Awesome 5 Free

Notice that in the above example the **file** names are different, but the registered **font** name is the same (use the **Font Book** application on Mac or the **Control Panel Fonts** section on Windows to see the actual font name). While this is no issue on Android, it renders the second font unusable on iOS. To handle similar cases additional CSS font properties, such as for example `font-weight`, must be added.

```CSS
/*
File name: fa-regular-400.ttf
Font name: Font Awesome 5 Free
*/
.far {
font-family: "Font Awesome 5 Free", "fa-regular-400";
font-weight: 400;
}

/*
File name: fa-solid-900.ttf
Font name: Font Awesome 5 Free
*/
.fas {
font-family: "Font Awesome 5 Free", "fa-solid-900";
font-weight: 900;
}
```


## Icon Font
![icomoon](../img/modules/icon-fonts/icomoon.png "Icomoon")

## Fonts Folder
![icon-fonts](../img/modules/icon-fonts/fonts-folder.png "Fonts Folder")
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// >> progress-setting-code
import { Component } from "@angular/core";
import { Progress } from "ui/progress";
import { Progress } from "tns-core-modules/ui/progress";

@Component({
moduleId: module.id,
Expand Down
2 changes: 1 addition & 1 deletion app/ng-ui-widgets-category/slider/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ button, label, stack-layout {
}

.icon {
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
}
/* << button-basic-style */

Expand Down
2 changes: 1 addition & 1 deletion app/ng-ui-widgets-category/switch/disable-switch/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ button, label, stack-layout {
}

.icon {
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* >> tabview-icon-font-ng-css */
.icon{
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
}
/* << tabview-icon-font-ng-css */
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* >> web-view-basic-css */
.icon{
font-family: 'icomoon';
font-family: 'IcoMoon-Free';
vertical-align: center;
margin: 6;
}
Expand Down