Auto sizing text elements
Automatically size text elements to fit their content.
Read time 1 minuteLast updated a month ago
You can enable auto sizing for text with Best Fit. The Best Fit feature allows you to set a minimum and maximum font size for the text element. The text element adjust its font size dynamically based on the content and available space within the range.
Best Fit also supports the following features:
- Word wrapping: Automatically wraps text to fit within the specified width.
- Ellipsis truncation: Automatically truncates text with an ellipsis (...) if it exceeds the available space.
- Multiple alignments: Supports left, center, and right alignment.
- Newline: Automatically inserts line breaks when necessary.
You can enable auto sizing in UI Builder, USS, or C# scripts.
In UI Builder
- Select the text element you want to modify.
- In the panel, select Text > Auto Size.Inspector
?
- Enter the minimum and maximum font sizes. The text element automatically adjusts its font size within this range to fit the text content.
In USS
In USS, set the property to with a minimum and maximum font size. For example:
-unity-text-auto-sizebest fit.labelText { -unity-text-auto-size: best-fit 5px 100px;}
In C# scripts
To enable auto sizing in C# scripts, use the property. For example:
TextAutoSizeModetextElement.style.unityTextAutoSize = new StyleTextAutoSize(new TextAutoSize(TextAutoSizeMode.BestFit, minSize: 10, maxSize: 100));