Align text of different text fields inside the Canvas UI

I have 2 text fields placed one below the other.
I want to ensure that the text string within each textfield always stays aligned in a line. For this purpose I use tab"/t" or “space”.

Eg -
Number: 24
AddingASecondNumber: 54

In the above example, “Number: 24” is in textfield1 and “AddingASecondNumber: 54” is in textfield 2.

Now I want 24 & 54 to appear one below the other. For this purpose I add extra space between “Number:” and 24.

However when the game resizes, the space that I have added inbetween the text string changes, therefore not aligning the text anymore.
For example on resizing the game, “24” in the first text field moves a little in front or back. Similarly others do as well.

How can I ensure that the text in each text field always stays stays aligned.

Looking forward to knowing the solution.

Has no one faced an issue of aligning text in text fields?
Looking forward to a solution.

Layout components will help you here.
If I was doing this, I wold create 2 text field objects for each line. So line one would be seperated into “Number” as one text field object and then “24” as the next text field object. I would then nest the two text fields inside a parent object and place a ‘horizontal layout group’ component on the parent object. I would then add ‘layout element’ components to the two text fields and adjust as necessary.
I would repeat the process for the 2nd line, and then nest both of the parent objects from both lines into another object and add whatever layout components are needed.

1 Like