Skip to main content
Question

Help needed: Adding Text to createShapeWithText 🛠️


Rob37

Hello Friends of Figma!

I’m building a Figjam plugin which generates shapes with dynamic text content, and I need your expertise to make it perfect!

I’m using the figma.createShapeWithText() function, which creates a nice shape just as expected. However, when it comes to adding text, I’m a bit stuck. I want to pass the randomText variable into the “characters” property, but no luck so far. 😕

Has anyone here cracked the code for a similar situation? Your valuable insights would mean a lot to me!

Here’s what I’ve done so far:

async function createRandomTextShape() {
  const x = 100;
  const y = 100;
  const randomText = "Aliqua consequat elit ipsum.";

  const shapeWithText = figma.createShapeWithText();
  shapeWithText.shapeType = "ROUNDED_RECTANGLE";
  shapeWithText.x = x;
  shapeWithText.y = y;

  [...]

  shapeWithText.fontSize = 12;
  shapeWithText.textAlignHorizontal = "CENTER";
  **shapeWithText.text.characters = randomText;**

  figma.currentPage.appendChild(shapeWithText);
}

Massive thanks

This topic has been closed for replies.

3 replies

tank666
  • 4873 replies
  • April 12, 2023

Before changing text properties, you need to load the font.

figma.com

Also you should change these lines:

shapeWithText.fontSize = 12;
shapeWithText.textAlignHorizontal = "CENTER";

to

shapeWithText.text.fontSize = 12;
shapeWithText.text.textAlignHorizontal = "CENTER";

Rob37
  • Author
  • 1 reply
  • April 14, 2023

Thanks a lot Yury! I managed to do it 😉

As you said I had to load the font before calling figma.createShapeWithText();


  • 0 replies
  • May 14, 2023

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings