Wrap text preemptively

Your method is not good, because it is based on characters count (characters have different size actually). So you need a way to measure a real width of text in UMG. Here’s my function, if anyone need it:

FString UGameFunctionLibrary::makeWrappedText(FString InString, float size, UTextBlock* textBlock) //UTextBlock* textBlock
{
	
	const TSharedRef<FSlateFontMeasure> FontMeasure = FSlateApplication::Get().GetRenderer()->GetFontMeasureService();

	int last_space = -1;
	int last_newline = -1;
	FString testLine;
	for (int i = 0; i < InString.Len(); i++) {
		if (InString[i] == ' ') {

			testLine = InString.Mid(last_newline + 1, i - last_newline);
			if (FontMeasure->Measure(testLine, textBlock->Font).X > size) {
				if (last_space >= 0) {
					InString[last_space] = '\n';
					last_newline = last_space;
				}
				else {
					InString[i] = '\n';
					last_newline = i;
				}
			}
			last_space = i;
		}

	}

	return InString;
}
2 Likes