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?
asked Oct 31 '16 at 07:42 AM in C++ Programming
So the hud does have a Get Text Size, but I don't that that is available outside of the HUD blueprint. https://docs.unrealengine.com/latest/INT/BlueprintAPI/HUD/GetTextSize/index.html
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.
While this is blueprint, this is just as easy to do for a string in CPP.
Im working on the same issue. But drawing debug text in scene.
The canvas does this under the hood:
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 :
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.
answered Sep 04 '18 at 05:40 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here