x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.13
Tags:
more ▼

asked Oct 31 '16 at 07:42 AM in C++ Programming

avatar image

wilberolive
318 30 45 48

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Hello,

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.

alt text

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

more ▼

answered Nov 08 '16 at 08:30 PM

avatar image

Motanum
630 58 65 306

avatar image wilberolive Nov 09 '16 at 10:26 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Sep 04 '18 at 05:40 PM

avatar image

HollingsworthDan
136 2 4 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question