How to calculate fontsize of same height by component height?

Viewed 103

There's a RectComponent, and a TextComponent inside. The text will change length sometimes.

So I need "text adaptation".

enter image description here

The fontsize :

The size of glyphs (in logical pixels) to use when painting the text.

I cannot find the conversion method between fontSize(logical pixels) and component size.

Is there some formula between "component size" and "font size"?

2 Answers

There is currently no way to automatically fit the text in the size of the RectangleComponent. You can however use TextComponent.textRenderer.measureWidth and calculate whether the font size has to be smaller or larger than it currently is.

Remember that if you don't use a mono-spaced font, a string with the same amount of characters as another string might become different widths.

Related