Skip to content

Commit d9c1605

Browse files
authored
docs: fonts documentation (#1)
1 parent b949fec commit d9c1605

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

content/project-structure/src/fonts.md

+72-10
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,91 @@
22
title: The Fonts folder
33
---
44

5-
Custom fonts are placed in the `src/fonts` folder. This folder is treated special, because `.ttf` files placed here are automatically picked up by NativeScript.
5+
A folder for storing custom fonts. Any `.ttf` files placed here are automatically picked up by NativeScript.
66

77
## Using Custom Fonts
88

9-
<!-- TODO: use concrete font as an example, and show ns fonts output -->
10-
11-
To use a custom font, place the `.ttf` file in the `src/fonts` folder, then run
9+
Place the `.ttf` file(s) in the `src/fonts` folder, then run
1210

1311
```cli
1412
ns fonts
1513
```
1614

17-
to generate the necessary css declarations. You can manually write these, the above command is just a convenience method to save some time and reduce the guesswork, since the `font-family` can be different from the file name.
15+
The result is a table with the font file name and the necessary CSS properties you can copy/paste into your css:
16+
17+
| Font | CSS Properties |
18+
| --------------------- | ------------------------------------------------------------------------- |
19+
| `albert-sans-700.ttf` | `font-family: "Albert Sans", "albert-sans-700"; font-weight: 700;` |
20+
| `fa-regular-400.ttf` | `font-family: "Font Awesome 5 Free", "fa-regular-400"; font-weight: 400;` |
1821

19-
Create a new css class in your CSS, for example in [app.css or app.scss](/project-structure/src/app-css-scss)
22+
Add a new css class (for example in [`app.css`](/project-structure/src/app-css-scss)) with the **CSS Properties** from the table (the name of the class can be anything)
2023

2124
```css
22-
.custom-font-class {
23-
font-family: '...';
25+
.font-albert-sans {
26+
font-family: "Albert Sans", "albert-sans-700";
27+
font-weight: 700;
28+
}
29+
30+
.far {
31+
font-family: "Font Awesome 5 Free", "fa-regular-400";
2432
font-weight: 400;
2533
}
2634
```
2735

28-
## Using Icon Fonts
36+
::: info Note
37+
Using `ns fonts` is optional, however we recommend using it because iOS and Android recognize `.ttf` files differently. On Android, the font is recognized by its **file name** while on iOS it is recognized by its **font name**. Fonts that have a different font name than the file name have to be registered with both names in the CSS rule.
38+
:::
39+
40+
You are ready to use the new fonts, apply the font class to an element and it will use the new font.
41+
42+
```xml
43+
<Label class="font-albert-sans" text="The quick brown fox jumps over the lazy dog"></Label>
44+
```
45+
46+
## Font Icons
47+
48+
In case of font icons, you will need to set the `text` to the correct char code of the icon, for example `&#xf019;`, or in case of an `Image` you can use the `font://` scheme as the `src`.
49+
50+
```xml
51+
<Label class="far" text="&#xf019;"></Label>
52+
<Image class="far" src="font://&#xf019;"></Image>
53+
```
54+
55+
::: warning Note for Vue users
56+
In Vue, when using html entities like the char codes above, use the `.decode` modifier on the property, in this case `text.decode` and `src.decode` to opt-into parsing HTML entities (disabled by default).
57+
58+
```xml
59+
<Label class="far" text.decode="&#xf019;" />
60+
<Image class="far" src.decode="font://&#xf019;" />
61+
```
62+
63+
:::
64+
65+
::: details Sizing font images
66+
Images have specific stretch options (`none`, `aspectFit`, `aspectFill`). Font icons on the other hand usually use `font-size` to control the size of the icon.
67+
When using an `Image`, you can control the size with `font-size`, but you need to set `stretch="none"` for it to take effect. Setting `stretch` to anything other than `none` will cause the icon to be streched by measuring the image.
68+
:::
69+
70+
If an icon doesn't appear, or renders unintended characters, make sure you are using the correct font family and weight.
71+
72+
## Fonts Icons vs Images
73+
74+
In many cases font icons are ideal replacements for images since they have a few drawbacks:
75+
76+
- pixelation/loss of quality when scaled up
77+
- may require additional http requests to be fetched
78+
- increase the application size when embedded
79+
80+
On the other hand, font icons:
81+
82+
- scale well
83+
- do not require additional http requests
84+
- do not increase memory usage significantly
85+
86+
However font icons have a limitation, they can only have a single color.
87+
88+
## Additional Resources
2989

30-
<!-- TODO: example of using an icon font -->
90+
- [Font Awesome 4 Cheatsheet](https://fontawesome.com/v4/cheatsheet/)
91+
- [Material icons](https://fonts.google.com/icons)
92+
- [Ionicons](https://ionic.io/ionicons)

0 commit comments

Comments
 (0)