1. 程式人生 > >mxGraph圖形文字自動換行

mxGraph圖形文字自動換行

兩種方式。

1、直接設定預設樣式

style[mxConstants.STYLE_WHITE_SPACE]='wrap'

2、重寫mxCellRenderer.prototype.createLabel方法,將其中state.text進行更改,原來的程式碼如下
        state.text = new mxText(value, new mxRectangle(), (state.style[mxConstants.STYLE_ALIGN] || mxConstants.ALIGN_CENTER), graph.getVerticalAlign(state), state.style[mxConstants.STYLE_FONTCOLOR], state.style[mxConstants.STYLE_FONTFAMILY], state.style[mxConstants.STYLE_FONTSIZE], state.style[mxConstants.STYLE_FONTSTYLE], state.style[mxConstants.STYLE_SPACING], state.style[mxConstants.STYLE_SPACING_TOP], state.style[mxConstants.STYLE_SPACING_RIGHT], state.style[mxConstants.STYLE_SPACING_BOTTOM], state.style[mxConstants.STYLE_SPACING_LEFT], state.style[mxConstants.STYLE_HORIZONTAL], state.style[mxConstants.STYLE_LABEL_BACKGROUNDCOLOR], state.style[mxConstants.STYLE_LABEL_BORDERCOLOR], graph.isWrapping(state.cell), graph.isLabelClipped(state.cell), state.style[mxConstants.STYLE_OVERFLOW]);

更改如下
var isWrap = true;
state.text = new mxText(value, rectangle, (state.style[mxConstants.STYLE_ALIGN] || mxConstants.ALIGN_CENTER),
                        graph.getVerticalAlign(state), state.style[mxConstants.STYLE_FONTCOLOR], state.style[mxConstants.STYLE_FONTFAMILY],
                        state.style[mxConstants.STYLE_FONTSIZE], state.style[mxConstants.STYLE_FONTSTYLE], 2, 1, 1, 1, 1, true,
                        null, state.style[mxConstants.STYLE_LABEL_BORDERCOLOR], isWrap, graph.isLabelClipped(state.cell),
                        state.style[mxConstants.STYLE_OVERFLOW]);

重寫mxCellRenderer.prototype.createLabel方法可以設定label的各種樣式。如果對label顯示有更多要求,建議重寫該方法。直接設定預設樣式更為簡單。