Wikis / Unreal Wiki / Legacy:Useful UWindow Extensions/Label

Auto-Sizing Checkbox

Labels (UMenuLabelControl) consists of a caption only.

Auto-sizing changes the width of the control without changing the location of the label.

With this piece of code you can make sure that Your labels do not use up more space than actually needed: to avoid oversized controls to cover other controls.

The width of the control (that You give at creation) is ingored in case the text is aligned to the left.

Here is the code:

class UMenuLabelControlAuto extends UMenuLabelControl;
 
var bool bAutoSize; // adjust the size of the control to its Text's size
 
function BeforePaint(Canvas C, float X, float Y)
{
	local float W, H;
	Super.BeforePaint(C, X, Y);
 
    if (bAutoSize) // Run autosize code
    {
        if (Text=="")
        {
            W=0;
            H=0;
			WinWidth=W;
			WinHeight=H;
        }
		else
		{
			TextSize(C, Text, W, H);
			WinHeight = H+1;
			TextY = (WinHeight - H) / 2;
			switch (Align)
			{
				case TA_Left:
					WinWidth=W+1;
					break;
				case TA_Center:
					WinLeft+=(WinWidth-W)/2;
					WinWidth=W;
					TextX=0;
					break;
				case TA_Right:
					WinLeft+=WinWidth-W;
					WinWidth=W;
					TextX=0;
					break;
			}
		}	
	}
	else // Run original UT code
	{
		TextSize(C, Text, W, H);
		WinHeight = H+1;
		TextY = (WinHeight - H) / 2;
		switch (Align)
		{
			case TA_Left:
				break;
			case TA_Center:
				TextX = (WinWidth - W)/2;
				break;
			case TA_Right:
				TextX = WinWidth - W;
				break;
		}	
	}
}
 
defaultproperties
{
	bAutoSize=false;
}

And here is how you include it in Your source:

	int XCoord, YCoord, Width;
	var UMenuLabelControlAuto Label;
	var localized string LabelText;
 
	// Some code here that fills in the XCoord, YCoord, Width and
	// LabelText values.
 
	Label=UMenuLabelControlAuto(CreateControl(class'UMenuLabelControlAuto', XCoord, YCoord, Width, 1));
	Label.bAutoSize=true;
	Label.Align=TA_Center;
	Label.SetText(LabelText);

Csimbi: Added clarification on width

Page Information

2022-11-18T15:58:22.978039Z 2003-12-09T10:12:19Z Esprx01x.nokia.com * https://wiki.beyondunreal.com/Legacy:Useful UWindow Extensions/Label Attribution-NonCommercial-ShareAlike 3.0