UMG: Scale Box and Wrap Text

Hi All,

The general thing I want to do is optimize the text in my widgets for localizations.
In particular I want to scale text of any length appropriately into a button image.

I can calculate the “Wrap Text At” size easily, but when using a scale box, it doesn’t scale the “Wrap Text At” size with it.

So my thoughts were:
I. Can I access the scale of the scale box in BP? Then I could set the “Wrap Text At” value more accurately
II. Can I somehow get the number of rows my wrapped text is at? X scaling works fine, but not Y. I figured a more or less accurate heuristic: (Initial Wrap Size) * #Rows = Wrap Text At Size

Can someone answer any of the above questions or has a different approach to this problem? Highly appreciate if you could share your thoughts!

I had a similar problem, what I did was:

  1. Translating texts using ( Shift + Enter ) for line break, so I wouldn’t need to use “Wrap Text
  2. Using Scale Box > Down only. So it’ll fit horizontally.

About your thoughts I did something similar in C++ for dialogs, but I’m not sure if it’s exposed in Blueprints.

1 Like

Thanks for your reply. Yes I was using “Down Only” as well, good hint!

Could you elaborate on point 1 a little bit please?
My classic example is the following:
I have a description widget with header and text body that appears when examining an object in the world. I usually like the widget to be of fixed size (i.e. dependent on screen resolution and not on text content). The english header would be “heater” (6 chars), translating to italian “riscaldamento” (13 chars). Same goes for simple buttons: english: “End” (3 chars), german: “Beenden” (7 chars).

I recently discovered the node “Wrapped Text Size” which could be very handy in determining the x-length of a given text and setting rules for that.

Since writing this post, I feel more confident in C++, so would you mind sharing your insights?

Thanks again!


.h

UCLASS()
class EXAMPLE_API UMyUserWidget : public UUserWidget
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintPure)
		float GetMyScaleBoxScale() const;

	UFUNCTION(BlueprintCallable)
		void SetMyScaleBoxScale(float Value);

protected:
	UPROPERTY(meta = (BindWidgetOptional))
		class UScaleBox* MyScaleBox;

};

.cpp


#include "MyUserWidget.h"
#include "Components/ScaleBox.h"

float UMyUserWidget::GetMyScaleBoxScale() const
{
	if (MyScaleBox)
	{
		return MyScaleBox->UserSpecifiedScale;
	}

	return 0.0f;
}

void UMyUserWidget::SetMyScaleBoxScale(float Value)
{
	if (MyScaleBox)
	{
		MyScaleBox->SetUserSpecifiedScale(Value);
	}
}
1 Like

Alright, I understand what you did there, Thanks!

In combination with the node “Wrapped Text Size”, I can calculate the scale needed to fit the text inside the buttons / text fields, thats a solid plan!

Actually I was thinking of the scale that the box decided to scale my text at currently which automatically changes whenever I change the text. I need to examine the source code of the scale box to see what kind of math it is doing and maybe manipulate it there

You can see the math in this function:

https://docs.unrealengine.com/4.27/en-US/API/Runtime/Slate/Widgets/Layout/SScaleBox/ComputeContentScale/

1 Like