issue in adding custom font in grapejs
Question
@artf i have followed the method below and successfully added the font in list but not all of the fonts are showing correct behavior, I want to add Avenir font in the system , When i added them it is not changing the font to Avenir but giving it some default value
editor.on('load', function () { styleManager = editor.StyleManager; fontProperty = styleManager.getProperty('Typography', 'font-family'); var list = []; fontProperty.set('list', list); list = [ fontProperty.addOption({value: "'Oswald', sans-serif", name: 'Oswald'}), fontProperty.addOption({value: "Helvetica Neue,Helvetica,Arial,sans-serif", name: 'Helvetica'}), fontProperty.addOption({value: "sans-serif", name: 'sans-serif'}), fontProperty.addOption({value: "Times New Roman", name: 'Times New Roman'}), fontProperty.addOption({value: "Arial Black", name: 'Arial Black'}), fontProperty.addOption({value: "'Avenir Book', Avenir", name: 'Avenir Book'}), fontProperty.addOption({value: "'Avenir Medium', Avenir", name: 'Avenir Medium'}), fontProperty.addOption({value: "'Avenir Roman', Avenir", name: 'Avenir Roman'}), fontProperty.addOption({value: "'Montserrat', sans-serif", name: 'Montserrat'}), fontProperty.addOption({value: "'Oswald', sans-serif", name: 'Oswald'}), fontProperty.addOption({value: "'Shadows Into Light', cursive", name: 'Shadows Into Light'}), fontProperty.addOption({value: "'Pacifico', cursive", name: 'Pacifico'}), fontProperty.addOption({value: "Tahoma", name: 'Tahoma'}), fontProperty.addOption({value: "Verdana, Geneva, sans-serif", name: 'Verdana'}), fontProperty.addOption({value: "Courier New Courier, monospace", name: 'Courier New Courier'}), fontProperty.addOption({value: "'Lato', sans-serif", name: 'Lato'}), fontProperty.addOption({value: "'Open Sans', sans-serif", name: 'Open Sans'}), fontProperty.addOption({value: "'Montserrat', sans-serif", name: 'Montserrat'}), ]; fontProperty.set('list', list); styleManager.render(); });
_Originally posted by @hgphpdev in https://github.com/artf/grapesjs/issues/857#issuecomment-443104577_
Answers (3)
Just do this
const prop = editor.StyleManager.getProperty('typography', 'font-family');
prop.set('options', [
{value: "'Oswald', sans-serif", name: 'Oswald'},
...
]);
and you should never doing this styleManager.render()
Just do this
const prop = editor.StyleManager.getProperty('typography', 'font-family'); prop.set('options', [ {value: "'Oswald', sans-serif", name: 'Oswald'}, ... ]);and you should never doing this
styleManager.render()
@artf thanku so much for this
i have replaced my code with this
i have successfully listed the fonts in the editor as show in the screenshot, but i want to add external CSS file to add local font in the editor. currently i m using the following method attached in screenshot.
is it the right method to import external css that i want to preload in the editor?? right now when i select this avenir font it is not changing to avenir
is it the right method to import external css that i want to preload in the editor??
Yeah, you're doing it right, you should see it. Check your font is loading correctly (wrong path, browser compatibility with .otf, etc.)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3111
Question: How to trigger something (function) on adding asset?
Hi, @artf. What is the correct way to trigger a function when an asset is added to the assets by providing the link and then pressing the '...
Issue #2715
I have added the custom font using the method below but that font is only working in few cases
There is one font issue when i click on any element it not show the current font of the element, i added custom code for add my custom font...
Issue #3213
QUESTION: How to save the dynamically added traits
Hi, @artf, I have a custom component that has a button trait. When the button is pressed, I am adding a custom trait dynamically using comp...
Issue #3119
[QUESTION] How to create custom block made of grapesjs components?
Hi, i want to have custom blocks made of grapesjs components. I found solution how to add components: https://github.com/artf/grapesjs/issu...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.