js-components-common-customizing-font.md 1.1 KB
Newer Older
E
ester.zhou 已提交
1
# Custom Font Styles
M
mamingshuai 已提交
2

Z
zengyawen 已提交
3
The custom font can be loaded from the font file in a project. The font file must be in .ttf or .otf format.
M
mamingshuai 已提交
4

E
ester.zhou 已提交
5 6 7 8 9 10
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.


## Defining @font-face
M
mamingshuai 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24

```
@font-face {   
  font-family: HWfont; 
  src: url('/common/HWfont.ttf'); 
}
```

**font-family**

Customize a font.

**src**

E
ester.zhou 已提交
25 26 27
Supported sources of custom fonts:

- Font file in the project: Specify the absolute path of the font file in the project through **url**. For details, see [File Access Rules](../../ui/js-framework-file.md).
M
mamingshuai 已提交
28

E
ester.zhou 已提交
29
- You can set only one **src** attribute.
M
mamingshuai 已提交
30 31


E
ester.zhou 已提交
32
## Using font-face
M
mamingshuai 已提交
33

E
ester.zhou 已提交
34
You can set **font-face** in **style** and specify the name of the **font-face** using **font-family**.
M
mamingshuai 已提交
35 36 37 38

**Example**

Page layout:
E
ester.zhou 已提交
39 40
```html
<!-- xxx.hml -->
M
mamingshuai 已提交
41
<div>    
E
ester.zhou 已提交
42
  <text class="demo-text">Test the custom font.</text>  
M
mamingshuai 已提交
43 44 45 46 47
</div>
```

Page style:

E
ester.zhou 已提交
48 49
```css
/*xxx.css*/
M
mamingshuai 已提交
50 51 52 53 54 55 56 57
@font-face {
  font-family: HWfont;
  src: url("/common/HWfont.ttf");
}
.demo-text {
  font-family: HWfont;
}
```