System fonts are built-in fonts of the operating system. They show text when no other font is chosen, maintaining clear and uniform text presentation.
System fonts can be enabled when an application does not register custom fonts or does not explicitly specify the text style. There are multiple system fonts. You can obtain the configuration information about system fonts and switch and use system fonts based on the font family name in the information.
Currently, system fonts cannot be disabled in ArkTS, but can be disabled in native code.
The following describes the common APIs and structs related to system fonts. The external APIs in ArkTS are provided by ArkUI. For details about the APIs, see @ohos.font.
| API | Description |
|---|---|
| getUIFontConfig() : UIFontConfig | Obtains the system font configuration. |
Import the required module.
import { font } from '@kit.ArkUI' Obtain the system font information.
let fontConfig = font.getUIFontConfig();
Print the font information in logs after obtaining the system font information.
console.info('sysFontMfg::font-dir -----------' + String(fontConfig.fontDir.length));
for (let i = 0; i < fontConfig.fontDir.length; i++) {
console.info(fontConfig.fontDir[i]);
}
console.info('sysFontMfg::generic-------------' + String(fontConfig.generic.length));
for (let i = 0; i < fontConfig.generic.length; i++) {
console.info('sysFontMfg::family:' + fontConfig.generic[i].family);
for (let j = 0; j < fontConfig.generic[i].alias.length; j++) {
console.info(fontConfig.generic[i].alias[j].name + ' ' + fontConfig.generic[i].alias[j].weight);
}
}
console.info('sysFontMfg::fallback------------' + String(fontConfig.fallbackGroups.length));
for (let i = 0; i < fontConfig.fallbackGroups.length; i++) {
console.info('sysFontMfg::fontSetName:' + fontConfig.fallbackGroups[i].fontSetName);
for (let j = 0; j < fontConfig.fallbackGroups[i].fallback.length; j++) {
console.info('sysFontMfg::language:' + fontConfig.fallbackGroups[i].fallback[j].language + ' family:' +
fontConfig.fallbackGroups[i].fallback[j].family);
}
} The following figure shows some system font configuration information of the application device system. The configuration information varies according to devices.

There are multiple system fonts. You can obtain the configuration information about system fonts and switch and use system fonts based on the font family name (fontFamilies in TextStyle).
If no font is specified, the default system font "HarmonyOS Sans" is used to display text.
Import the required module.
import { text } from '@kit.ArkGraphics2D'; Create TextStyle1 and set fontFamilies to HarmonyOS Sans SC, which is the default Chinese font.
let textStyle1: text.TextStyle = {
color: { alpha: 255, red: 255, green: 0, blue: 0 },
fontSize: 100,
fontFamilies: ['HarmonyOS Sans SC']
}; Create TextStyle2 and set fontFamilies to HarmonyOS Sans TC (to compare the difference in the same text typeface).
let textStyle2: text.TextStyle = {
color: { alpha: 255, red: 255, green: 0, blue: 0 },
fontSize: 100,
fontFamilies: ['HarmonyOS Sans TC']
}; Create a paragraph generator.
// Create a paragraph style object to set the typography style.
let myParagraphStyle: text.ParagraphStyle = {
textStyle: textStyle1,
align: 3,
wordBreak: text.WordBreak.NORMAL
};
// Obtain the global font set instance.
let fontCollection = text.FontCollection.getGlobalInstance();
// Create a paragraph generator.
let paragraphBuilder = new text.ParagraphBuilder(myParagraphStyle, fontCollection); Add textStyle1 and textStyle2 to the paragraph style and add the text.
let str: string = 'Module description\n'; // Add the first text style and the corresponding text content. paragraphBuilder.pushStyle(textStyle1); paragraphBuilder.addText(str); // Add the second text style and the corresponding text content. paragraphBuilder.pushStyle(textStyle2); paragraphBuilder.addText(str);
Generate a paragraph for future drawing.
let paragraph = paragraphBuilder.build();
The following figure shows the effect.
