Skip to main content
Solved

loadFontAsync Multiple Fonts (MEMORY ERROR)


Andre_Pinto

Hi Figma devs 😃

I’m trying to load all fonts before updating layer character.

const fontArray = [
  { family: 'Roboto', style: 'Regular' },
  { family: 'Roboto', style: 'Bold' },
  { family: 'Font Awesome 5 Pro', style: 'Solid' },
  { family: 'Font Awesome 5 Pro', style: 'Light' },
];

fontArray.map(async (font) => {
  await figma.loadFontAsync(font);
});

Unfortunately, I get a memory error. Can somebody help, please?
Thanks in advance

Best answer by Andre_Pinto

I got it working already 😀 Thank you for the quick responses.

I’m doing it like this:

await figma.loadFontAsync(node.fontName as FontName);
node.characters = value;
View original

5 replies

Gleb
  • Power Member
  • 4706 replies
  • April 22, 2021

You can’t use async in a map so this works synchronously and likely the fonts aren’t loaded before you try to change the characters. Also all these four calls would happen at the same time, a memory error may be caused by trying to load multiple fonts at the same time.


Karl_Koch
  • 14 replies
  • April 22, 2021

Why not load all fonts without manually specifying them?

selection.forEach(async (font) => {
await figma.loadFontAsync(font.fontName as FontName);
});

Andre_Pinto
  • Author
  • 3 replies
  • Answer
  • April 22, 2021

I got it working already 😀 Thank you for the quick responses.

I’m doing it like this:

await figma.loadFontAsync(node.fontName as FontName);
node.characters = value;

Karl_Koch
  • 14 replies
  • April 22, 2021

Smashing! This took me an age to figure out myself haha.


laurilllll

Hi!

I got the fonts working like this:

// Function for loading all the needed fonts
const loadFonts = async () => {

  await figma.loadFontAsync({ family: "Work Sans", style: "Regular" })
  await figma.loadFontAsync({ family: "Work Sans", style: "Medium" })
  await figma.loadFontAsync({ family: "Work Sans", style: "Bold" })

  console.log("Awaiting the fonts.")

}

// Load the fonts by running the function
loadFonts().then(() => {

  // Make them useful
  doSomethingElseWithThem()
  console.log("Fonts fetched and ready to go!")

})

Maybe this helps someone!


Reply


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