Javascript 如何通过XD中的插件获得文本元素的宽度值?

Javascript 如何通过XD中的插件获得文本元素的宽度值?,javascript,adobe-xd,Javascript,Adobe Xd,我正在尝试为XD构建一个插件来自动创建设计元素。我已经编写了一个函数来使用文本元素和矩形构建按钮(下面的第一张图片)。唯一的问题是,矩形的形状不是基于文本的宽度,因此当文本较长时,文本与矩形重叠(参见下面的第二张图片) 我一直在试图找出如何获得文本元素的宽度值,以便能够适当地调整矩形的大小。有人能帮我吗 通过添加行console.log(selection.items),我发现了这一点我可以将完整的属性列表输出到日志,但是如何访问width属性 下面是显示矩形和文本元素的日志输出 [ Tex

我正在尝试为XD构建一个插件来自动创建设计元素。我已经编写了一个函数来使用文本元素和矩形构建按钮(下面的第一张图片)。唯一的问题是,矩形的形状不是基于文本的宽度,因此当文本较长时,文本与矩形重叠(参见下面的第二张图片)

我一直在试图找出如何获得文本元素的宽度值,以便能够适当地调整矩形的大小。有人能帮我吗

通过添加行console.log(selection.items),我发现了这一点我可以将完整的属性列表输出到日志,但是如何访问width属性

下面是显示矩形和文本元素的日志输出

[ Text ('Submit Button') {
    width: 113, height: 20
    global X,Y: -3, -74
    parent: Artboard ('iPad – 1')
    fill: ff000000
  },
  Rectangle ('Rectangle 6') {
    width: 100, height: 50
    global X,Y: -3, -58
    parent: Artboard ('iPad – 1')
    stroke: ff2d3494
  } ]
这是我的全部代码

const {Rectangle, Color, Text, Selection} = require("scenegraph"); 
let commands = require("commands");

function createButton(selection) { 

    // Create the outline of the button
    const newButtonOutline = new Rectangle(); 
    newButtonOutline.width = 100;
    newButtonOutline.height = 50;
    newButtonOutline.stroke = new Color("#2D3494");
    newButtonOutline.strokeWidth = 2;
    newButtonOutline.cornerRadii = { 
        topLeft: 10, 
        topRight: 10, 
        bottomRight: 10, 
        bottomLeft: 10
    };

    // Create the text for the button
    const newButtonLabel = new Text();
    newButtonLabel.text = "Submit Button";
    newButtonLabel.fontSize = 18;
    newButtonLabel.fill = new Color("#000000");


    // Add the text and outline to the artboard
    selection.insertionParent.addChild(newButtonOutline);
    newButtonOutline.moveInParentCoordinates(100, 100);
    selection.insertionParent.addChild(newButtonLabel);
    newButtonLabel.moveInParentCoordinates(100, 100);
    selection.items = [newButtonLabel, newButtonOutline];
    console.log (selection.items);

    //align the button elements and group together 
    commands.alignHorizontalCenter();
    commands.alignVerticalCenter();
    commands.group();

}

我希望有人能帮忙

结果是要获得文本节点的宽度,您需要从localBounds获取它,类似于

textNode.localBounds.width

或者要获得图形节点的宽度,只需

图形节点宽度


我已经计算出,下面将输出矩形的宽度(100),但在文本上它表示未定义。。。?console.log(selection.items[1]['width']);