【问题标题】:Creating font-family drop-down in Redactor WYSIWYG在 Redactor 所见即所得中创建字体系列下拉菜单
【发布时间】:2012-11-07 00:35:37
【问题描述】:

我正在尝试在Redactor 编辑器中创建一个下拉菜单。最大的问题是使用所选字体系列围绕所选文本创建一个容器。

到目前为止,我已经完成了自定义下拉菜单的基本设置:

$("#text_edit").redactor({
    buttons: ['html', '|', 'bold', 'italic', 'deleted', '|', 'table', 'link', '|', 'fontcolor', 'backcolor', '|', 'fontfamily'],
    buttonsCustom: {
        fontfamily: {
            title: "Select Font",
            dropdown: {
                Arial: {
                    title: 'Arial',
                    callback: insertFont
                },
                Georgia: {
                    title: 'Georgia',
                    callback: insertFont
                }
            }
        }
    }
});

function insertFont(obj, e, key)
{
    // wrap selected text in <span> container with style attribute and selected font
}

实际上想要的方法与内置的 fontcolor 函数非常相似,它也将选中的文本包装在一个容器中,并为其分配正确的颜色样式属性。

【问题讨论】:

标签: jquery drop-down-menu wysiwyg text-editor rich-text-editor


【解决方案1】:

在上一个响应的基础上进行改进,这是 JQuery 中的一个实现,它将在下拉列表中很好地列出所有可能的网络安全字体:

var oFontMap: {
arial: ["Arial", "Arial, Helvetica, sans-serif"],
arialblack: ["Arial Black", '"Arial Black", Gadget, sans-serif'],
comicsans: ["Courier New", '"Courier New", Courier, monospace'],
courier: ["Comic Sans", '"Comic Sans MS", cursive, sans-serif'],
impact: ["Impact", 'Impact, Charcoal, sans-serif'],
lucida: ["Lucida", '"Lucida Sans Unicode", "Lucida Grande", sans-serif'],
lucidaconsole: ["Lucida Console", '"Lucida Console", Monaco, monospace'],
georgia: ["Georgia", "Georgia, serif"],
palatino: ["Palatino Linotype", '"Palatino Linotype", "Book Antiqua", Palatino, serif'],
tahoma: ["Tahoma", "Tahoma, Geneva, sans-serif"],
times: ["Times New Roman", "Times, serif"],
trebuchet: ["Trebuchet", '"Trebuchet MS", Helvetica, sans-serif'],
verdana: ["Verdana", "Verdana, Geneva, sans-serif"] };

//Create the font dropdown
var oFontDropdown = {}
$.each(oFontMap, function(iIndex, oFont){
    var sFontName = oFont[0];
    var sFontFace = oFont[1];
    oFontDropdown[iIndex] = {
        title: "<font face='"+sFontFace+"'>"+sFontName+"</font>",
        callback: function(obj, e, sFont){
            obj.execCommand("fontname", sFontFace);
        }
    }
});

return $(".redactor").redactor({
    focus: true,
    buttonsAdd: ["|", "font"],
    buttonsCustom: {
        font: {
            title: "Advanced Font List",
            dropdown: oFontDropdown
        }
    }
});

【讨论】:

    【解决方案2】:

    你可以试试这个,我已经解决了同样的要求,就是这样,在这里运行良好。

    var setFontFamily;
    
    setFontFamily = function(obj, e, key) {
      if (key === "serif") {
        obj.execCommand("fontname", "Times New Roman");
      }
      if (key === "sans") {
        return obj.execCommand("fontname", "Helvetica");
      }
    };
    
    $(document).ready(function() {
      return $(".redactor").redactor({
        focus: true,
        buttonsAdd: ["|", "font"],
        buttonsCustom: {
          font: {
            title: "Advanced List",
            dropdown: {
              serif: {
                title: "Times",
                callback: setFontFamily
              },
              sans: {
                title: "Helvetica",
                callback: setFontFamily
              }
            }
          }
        }
      });
    });
    

    setFontFamily 函数并不是世界上最漂亮的,但它是一个概念证明,然后你可以看到它是如何定义字体系列的。

    我是一个 Rails 编码员,所以我会在这里留下相同的代码,但 coffescript 版本(以防万一有人需要)

    setFontFamily = (obj, e, key) ->
      obj.execCommand "fontname", "Times New Roman"  if key is "serif"
      obj.execCommand "fontname", "Helvetica"  if key is "sans"
    
    $(document).ready ->
      $(".redactor").redactor
        focus: true
        buttonsAdd: ["|", "font"]
        buttonsCustom:
          font:
            title: "Advanced List"
            dropdown:
              serif:
                title: "Times"
                callback: setFontFamily
              sans:
                title: "Helvetica"
                callback: setFontFamily
    

    【讨论】:

      【解决方案3】:

      Redactor 为此发布了新插件:

      fontcolor.js, fontsize.js and fontfamily.js
      

      包含后,您可以像这样使用它们:

      elem.redactor({
            plugins: ['fontcolor', 'fontsize', 'fontfamily']
      });
      

      查看此存储库: https://github.com/johnsardine/redactor-plugins

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-18
        • 2014-02-25
        • 2011-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        相关资源
        最近更新 更多