如何在font.css中添加字体以便在CKEDITOR中使用字体

How to add font in font.css for using fonts in CKEDITOR

本文关键字:字体 CKEDITOR 添加 font css      更新时间:2023-09-26

我想在ckeditor中动态添加字体。为此,我使用config.contentsCss="。是静态执行的。我不知道如何在外部文件中添加css,或者有另一种方法添加@font-face到ckeditor。我的代码是这样的:

var fontStyle = '';
        $.ajax({
            url : "/chrysofin/fonts/getFonts",
            success : function(response) {
                $.each(response, function(index, item ) {
                    var fontName = item.name;
                    var eotFile = item.eotPath;
                    var ttfFile = item.ttfPath;
                    fontStyle +=  "@font-face {'n" +
                                        "'tfont-family: ' "+ fontName+"';'n" + 
                                        "'tsrc:  url('../invitations/fonts/"+ eotFile +"');'n" + 
                                        "'tsrc:  url('../invitations/fonts/"+ ttfFile +"') format('truetype');'n" + 
                                    "}'n" ;
                });
                alert(fontStyle);
                /*CKEDITOR.config.customConfig = function(config) {
                    config.contentsCss = fontStyle;
                };*/
//              CKEDITOR.addCss(fontStyle);
                $.each(response, function(index, item ) {
                    var fontName = item.name;
                    CKEDITOR.config.font_names = fontName +'/'+fontName + ';' + CKEDITOR.config.font_names;
                });
            },
            error : function() {
                displayError("Error while getting fonts.");
            },
        });
//  CKEDITOR.config.contentsCss ='../assets/invitations/fonts/fonts.css';
    CKEDITOR.config.font_names = 'Windsong/windsong;' + CKEDITOR.config.font_names;

我也遇到了同样的问题。您可以在服务器端创建一个css文件,并在该文件中编写css。之后,将此文件路径传递给CKEDITOR.config.contentsCss = "Path/of/file"