Dynamically reduce text font size or scale to fit parent container?

  • I need a single line of text to fit within a container without wrapping, breaking the boundary, or truncating.
  • I need the text to get smaller when it gets too long to fit, either by reducing font size or overall scale.
  • The text is dynamic, so I can’t reliably tell how long it will be to pick an ideal font size.

Things I’ve tried:

  • Standard left and right with top and bottom constraints, but if the text becomes too long, it wraps and breaks the boundary or truncates.
  • Using text auto width with scale constraints, but the text just grows outside the boundary.
  • Using text auto width then wrapping it with an auto layout, thinking the layout could hug the text and use scale constraints to get smaller, but hug resizing and scale constraints are mutually exclusive.

Is there any way to do this dynamically?

1 Like

In Figma, achieving dynamic text resizing based on the container without wrapping, breaking the boundary, or truncating can be a bit tricky, but it is possible. Here’s a potential solution:

  1. Auto Layout Container:
  • Create an Auto Layout frame that will act as your text container.
  • Place your text layer inside this Auto Layout frame.
  1. Auto Width:
  • Set the width of the text layer to Auto Width. This will allow the text to expand or contract based on its content.
  1. Auto Layout Constraints:
  • Ensure that your text layer has constraints set within the Auto Layout frame. These constraints will determine how the text behaves when resizing.
  1. Scale Constraints:
  • Apply scale constraints to the text layer. You can do this by selecting the text layer, holding down the Shift key, and dragging one of the corner handles to resize the text layer. Hold down the Shift key to maintain the aspect ratio.
  1. Adjustable Font Size:
  • For dynamic text resizing, use the Auto Layout properties and enable “Shrink” under the “Resize” section in the Auto Layout settings for the text layer.

By following these steps, the text should resize dynamically based on the container without wrapping or breaking the boundary. The “Shrink” option in Auto Layout will make the font size decrease as needed.

Remember to test this with different lengths of text to ensure it behaves as expected. Figma might have introduced new features or changes since my last update, so it’s always a good idea to check the latest documentation or community forums for any updates.

Thanks for your reply, but I can’t seem to follow what you suggested, starting with Step 3…

  1. Constraints seem to be disabled on objects inside an Auto Layout frame. All I have are Resizing Options on the Text Layer. Selecting anything other than Hug changes the text’s Auto Width to something else, contradicting Step 2.

  2. As with Step 3, Constraints are disabled on objects inside an Auto Layout frame, so I can’t apply Scale Constraints. Holding Shift and dragging one of Text Layer’s corners just changes the Resizing Options from Hug to Fixed, which also changes the text’s Auto Width to Fixed Size, again contradicting Step 2.

  3. I’m not seeing any Auto Layout properties on the Text Layer, just the typical Resizing Options I mentioned earlier. I’ve never seen a “Shrink” option in regards to text.

Here’s a screenshot of my Design Panel for the Text Layer:

Can you maybe send some screenshots where these options are?

Thanks again, much appreciated.

Hi.

I don’t think you can dynamically scale the font size of a Text element in Figma by changing/scaling the size of it’s parent layer, or maybe not yet, or maybe I am wrong.

What you can do if you want your text to a single line and fits the container without wrapping, breaking the boundary, or truncating the text is by using this technique:

  1. Through Auto-layout - Set the text element to auto-width and the auto-layout container to hug. The container will just adjust it’s width depending on the length of the text layer without wrapping or breaking it but the problem is you won’t be able to adjust it’s width or length manually else the hug properties will be removed and become fixed.

  2. Scale - If you want to manually scale the container where the text will also scale dynamically, you can use the scale tool in Figma. You can find it on the tools menu on the upper left corner of the app.
    image

Thanks Raphael14, appreciate your time.

I am familiar with that setup, where the text drives the parent container size, but in this case I was hoping to reverse that relationship, and have the parent container drive the text size. Can’t seem to figure out a way to do that.

Thanks though!

Consider using an UILabel with adjustsFontSizeToFitWidth set to true, and add constraints for left, right, top, and bottom. Combine this with a minimum font scale and ensure the content hugging priority is lower than the compression resistance priority. This allows dynamic text adjustment within the container without wrapping or breaking boundaries.

Those look like Swift commands. I don’t see how to use them in Figma.

Am I missing something? Can you show me how to apply this to a Figma design?