如何为确定的字符复制 IText 样式

How can I copy IText styles for definite chars

例如,我们有一个 IText“text1”,从第 2 个字符到第 4 个字符带有下划线。

我们需要像这样将它的完整样式复制到“text2”:

所以这里我们必须为“text1”插入一些函数,如 getStyleDeclaration 或 getSelectionStyles,为“text2”插入 setSelectionStyles,但我仍然无法使其工作。

var text1 = new fabric.IText('text1', {
    left: 100,
    top: 100,
});

text1.setSelectionStyles({ underline: true, }, 2, 4);

var text2 = new fabric.IText('text2', {
    left: 200,
    top: 200,
});

function createCanvas(id) {
    canvas = new fabric.Canvas(id);
    canvas.add(text1);
    canvas.add(text2);
    return canvas;
}

您可以只在文本对象中使用 styles 属性

const styles = text1.styles;

top-level 属性 -> 行号,二级属性 - 字符号

在你的情况下你会得到

{
  "0": { // line number
    "2":{ // charater number
      "underline":true
    },
    "3":{ // charater number
      "underline":true
    }
  }
}

http://fabricjs.com/docs/fabric.Text.html#styles