UGTK / Toolkit / Ui Module / TMP Line Balancer Completed
TMP Line Balancer
Summary
Mn_TMPTextHardLineBalancer re-flows a TextMeshProUGUI string into a fixed (or ranged) number of visually balanced lines by inserting hard newlines at word boundaries. It normalizes spaces, strips existing newlines, and enforces NoWrap + a chosen overflow mode so TMP never adds extra lines on its own.
Content
Modules Dependencies
This module has no hard UGTK dependencies. It requires TextMesh Pro (TMPro).
Setup
Drop the Text_LineBalancer prefab under a Canvas — a TMP label with the balancer already
attached:
How To Use
- Add
Mn_TMPTextHardLineBalancernext to aTextMeshProUGUI; onReset/Awakeit auto-assignstargetTextto the component on the same GameObject. - Choose
lineMode:Fixed(fixedLineCount) orRange(minLines/maxLines/currentLines).centerBiastunes how evenly line lengths are balanced. - Options:
normalizeSpaces,stripExistingNewlines,minCharsToBalance(below this, text is left on one line), plusforceDisableWordWrappingandforceOverflowMode/overflowModeto keep TMP from wrapping. - With
autoReformaton, the balancer watches the text every frame and re-formats when it changes. Public API:SetText(string),SetMaxLines(int),GetMaxLines(), andForceReformat(). - A hard guarantee clamps the output to at most
lines - 1newlines.
Good to know:
- It rewrites the label's text. The newlines are real characters in
text: reading it back gives you the reflowed string, not the original. The original is kept in the read-only debug field. - It forces TMP's wrapping off and overflow to Truncate/Ellipsis (
forceDisableWordWrapping,forceOverflowMode): that is what guarantees the line count, but it also means a line that is too long for the rect gets cut, not wrapped. Size the rect for the longest line, or raise the line count. autoReformatpolls the text every frame. On a handful of headlines that is nothing; do not put it on a list of hundreds of labels — callForceReformat()when the text changes instead.- Below
minCharsToBalancethe text is left on one line, which avoids the ugly two-word, two-line result on short strings.
Testing Scene
Open Debug/TMPLineBalancerScene.unity: the same sentence is shown balanced over a different number
of lines, so the effect of lineMode and centerBias is visible side by side.
Technical Info
- Mn_TMPTextHardLineBalancer:
[ExecuteAlways]MonoBehaviour that normalizes the source text, computes balanced word-boundary cuts toward a per-line target length, inserts hard newlines, clamps the line count, and enforces TMP wrapping/overflow settings.
© 2026 Marcello De Bonis. All rights reserved
UGTKengine within an engine


