How to measure text length?

I have a UTextRenderComponent on an actor and I need to display some text in it that must fit within a certain space in the world. So I need to dynamically scale the size of the text depending on its length to ensure it always fits in the space. So I’m wondering it there are any functions built in to UE4 that will tell me the length of a string of text rendered with a given font for example?

Hello,

So the hud does have a Get Text Size, but I don’t that that is available outside of the HUD blueprint.

What I did as a workaround, was to get the length of the string, how many chars, after I add a 1, to have some extra padding for really short strings. Then I multiply that by the average size of a char in cm. Seems like a good enough workaround.

http://puu.sh/sb8Ji/5f722ac1ee.png

While this is blueprint, this is just as easy to do for a string in CPP.

int32 sSize = (myFString.len() + 1 ) * 8 //Using 8 as average char size in world, could use a parameter

I believe UMG can also measure text too, but doesn’t help. I’ll give your suggestion a go and see how it turns out.

Im working on the same issue. But drawing debug text in scene.

The canvas does this under the hood:

DrawFont->GetCharSize(...);
CharSpacing += DrawFont->GetCharKerning( *pPrevChar, Ch ) * ScaleX;

And if you look at the function Canvas->GetDefaultCharSize() you will see they just loop over all the characters in the font and average them out.

The DrawDebugString() api does not take a font so… digging down through that we get :

UFont* DrawFont = GEngine->GetSmallFont();

Putting it all together, you loop over all of your text. The size is equal to the size of each character to display, and the Kerning between each character.

Dear Community,

To Measure the size of a Textblock, RichTextBlock, or any widget actually, including font and render scaling, you can use this!

In this example I am retrieving the horizontal center of the widget!

This DOES work in all screen resolutions, including the very moment the screen resolution has changed!

:heart:

Rama

1 Like