Custom Font List
Font structure definition
In Univer, the IFontConfig interface describes a font:
| Property | Type | Description |
|---|---|---|
value | string | The unique identifier of the font, usually the first entry in a CSS font-family. |
label | string | Translation key. If the key has no translation when the Univer instance is created, the supplied value is shown unchanged. |
category | FontCategory | Font category, which groups fonts in the UI. Optional. |
Univer does not prevent users from selecting a font that is not installed. If the client does not have the font, the browser substitutes its default font.
Using custom fonts
The customFontFamily parameter on UniverUIPlugin controls which options the font dropdown offers.
UniverUIPlugin
containerstring | HTMLElementThe element to mount into, or the DOM ID of that element.customFontFamilyIFontConfig[] | objectAdds these font choices to the dropdown.listIFontConfig[]Adds these font choices to the dropdown.valuestringFont family name used in CSS.labelstringDisplay name or translation key.categoryFontCategoryFont group. Usesans-serif,serif,monospace,display, orhandwriting.
overridebooleanDefault:falseReplaces the built-in choices with the supplied list.
TypeScript
import { FontCategory, UniverUIPlugin } from '@univerjs/ui'univer.registerPlugin(UniverUIPlugin, { container: 'app', customFontFamily: { list: [ { value: 'PingFang SC', label: '苹方(简)', category: FontCategory.SANS_SERIF, }, { value: 'Helvetica Neue', label: 'Helvetica Neue', category: FontCategory.SANS_SERIF, }, ], // Whether to override the default font list, defaults to false override: true, },})Facade API
Fonts do not have to be known at startup. The Facade API can add new fonts at runtime, which is required when font resources load asynchronously.
TypeScript
import { FontCategory } from '@univerjs/ui'univerAPI.addFonts([{ value: 'PingFang SC', label: 'PingFang SC', category: FontCategory.SANS_SERIF,}, { value: 'Helvetica Neue', label: 'Helvetica Neue', category: FontCategory.SANS_SERIF,}])How is this guide?