如何使用tspan拆分Ext.draw.text中的长文本

How to split long text in Ext.draw.Text using tspan

本文关键字:文本 text draw 何使用 tspan 拆分 Ext      更新时间:2023-09-26

我使用Ext.draw.Text为Ext图表创建了一个自定义图例面板……在我的应用程序中,图例可能有很长的自定义文本。所以我需要用一个固定的长度把它们分开。一种方法是使用Javascript分割文本,并为文本的每个部分创建文本精灵。

有其他方法可以在Ext.draw.text中拆分文本吗?

代码:

Ext.create('Ext.draw.Component', {
    renderTo: Ext.getBody(),
    width: 800,
    height: 200,
    items: [{
        type: 'rect',
        fill: 'red',     
        width: 12,
        height: 12,
        x: 5,
        y: 10
    },{
        type: "text",
        text: "This is a long text.. Can i split this into two tspan",
        fill: "green",
        width: 12,
        height: 12,
        font: "18px monospace",        
        x: 25,
        y: 15
    }]
});

提前感谢。

通过在文本内容中插入换行符('''n'),我成功地为Ext.draw.text生成了多个tspan。JSFiddle

Ext.create('Ext.draw.Component', {
    renderTo: Ext.getBody(),
    width: 800,
    height: 200,
    items: [{
        type: 'rect',
        fill: 'red',     
        width: 12,
        height: 12,
        x: 5,
        y: 10
    },{
        type: "text",
        text: "This is a long text.. 'n Can i split this into two tspan", //Added new line character
        fill: "green",
        width: 12,
        height: 12,
        font: "18px monospace",        
        x: 25,
        y: 15
    }]
});