Custom Font List
Font structure definition
A font is described by the IFontConfig interface:
| Property | Type | Description |
|---|---|---|
value | string | The unique identifier of the font, usually the value to use for CSS font-family. |
label | string | The translation key used for internationalization. If the key is not specified when the Univer instance is created, the supplied value is displayed directly. |
category | FontCategory | Font category, used to group entries in the UI. Optional. |
Univer does not prevent users from selecting a font that is not installed. If the client does not have that font, the browser falls back to the default font.
Using custom fonts
Configure customFontFamily on UniverSheetsCorePreset (Preset Mode) or UniverUIPlugin (Plugin Mode) to set the choices in the font dropdown.
Plugin configuration
UniverUIPlugin
containerstring | HTMLElementElement to mount into, or its DOM ID.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, },})Preset configuration
UniverSheetsCorePreset
containerstring | HTMLElementDefault:'app'Element to mount into, or its DOM ID.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 } from '@univerjs/ui'const { univerAPI } = createUniver({ presets: [ UniverSheetsCorePreset({ 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 configured at startup. They can also be added at runtime through the Facade API, which is useful when font files 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?