Custom Fonts
DivKit allows you to use custom fonts in your application. This makes it possible to maintain a consistent text style across platforms and ensure compliance with your application's design.
General Concept
To use custom fonts in DivKit, you need to:
- Add fonts to your application resources
- Implement a font provider for the corresponding platform
- Pass the font provider to DivKit during initialization
- Use font names in DivKit JSON markup
In DivKit JSON markup, fonts are specified in the font_family parameter of the div-text element:
Sample code
{
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 500,
"text": "Text with custom font"
}
The font_weight parameter accepts the light, regular, medium, and bold values. To set the font weight precisely, use the font_weight_value parameter: a positive integer. Typical values:
- 100 - Thin
- 200 - Extra Light
- 300 - Light
- 400 - Regular (default value)
- 500 - Medium
- 600 - Semi Bold
- 700 - Bold
- 800 - Extra Bold
- 900 - Black
Font Variation Settings
For variable fonts, you can use the font_variation_settings property to have fine-grained control over font characteristics. This property allows you to specify custom values for font variation axes.
Usage
The font_variation_settings property accepts a dictionary where keys are 4-character axis tags and values are numeric settings:
Sample code
{
"type": "text",
"font_family": "RobotoFlex",
"font_variation_settings": {
"wght": 620,
"slnt": -7
},
"text": "Text with custom font variations"
}
Common Variation Axes
wght- Weight (thickness of strokes)wdth- Width (condensed to expanded)slnt- Slant (oblique angle)ital- Italic (0 for upright, 1 for italic)opsz- Optical size
Interaction with font_weight
When both font_weight and font_variation_settings with wght axis are specified, the font_variation_settings takes precedence for variable fonts, providing more precise weight control.
Example with different weight specifications
{
"type": "container",
"items": [
{
"type": "text",
"font_family": "RobotoFlex",
"font_weight": "medium",
"text": "Text with font_weight: medium"
},
{
"type": "text",
"font_family": "RobotoFlex",
"font_weight_value": 620,
"text": "Text with font_weight_value: 620"
},
{
"type": "text",
"font_family": "RobotoFlex",
"font_variation_settings": {
"wght": 620
},
"text": "Text with font_variation_settings wght: 620"
},
{
"type": "text",
"font_family": "RobotoFlex",
"font_variation_settings": {
"wght": 620,
"slnt": -7
},
"text": "Text with weight 620 and slant -7"
}
]
}
Connecting fonts in the app
To turn the name from font_family into a font, the app passes a font provider to DivKit. The implementation for Android, iOS, and Web is described in Resources: fonts, images, and video.
Usage Examples in JSON
Basic Example
Sample code
{
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 400,
"text": "Text with custom font"
}
Different Font Styles
Sample code
{
"type": "container",
"items": [
{
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 300,
"text": "Text with light font (Light)"
},
{
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 400,
"text": "Text with regular font (Regular)"
},
{
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 500,
"text": "Text with medium font (Medium)"
},
{
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 700,
"text": "Text with bold font (Bold)"
}
]
}
Using in Templates
Sample code
{
"templates": {
"title": {
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 700,
"font_size": 20,
"$text": "title"
},
"body": {
"type": "text",
"font_family": "MyCustomFont",
"font_weight_value": 400,
"font_size": 16,
"$text": "text"
}
},
"card": {
"log_id": "custom_fonts_example",
"states": [
{
"state_id": 0,
"div": {
"type": "container",
"items": [
{
"type": "title",
"text": "Heading with custom font"
},
{
"type": "body",
"text": "Body text with custom font"
}
]
}
}
]
}
}
Learn more
You can discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_en.