Issues with centering text

Hey developers! I am trying to make a nametag system similar to that of @Merely in Trade Hangout. You can play what I have so far here:
https://www.roblox.com/games/6587724076/Club-Neo-INDEV

The main issue I am having is centring the text. Here’s my issue in bullet points:

  • Every letter must be it’s own label or I can’t tween each letter induvidually.
  • I can’t use a UIListLayout because then I cant tween the position of the labels.
  • I can’t have every label the same size and the same distance apart or the nametags will be wonky.

Here’s my current script for this:

local function RefreshNametag(Character)
		if Character.PrimaryPart:FindFirstChild("Nametag") then Character.PrimaryPart:FindFirstChild("Nametag"):Destroy() end
		local Nametag = script.Nametag:Clone()
		Nametag.Size = UDim2.fromScale(#Player.DisplayName/2.5, 1.5)

		local Labels = {}

		local Count = 1

		for i = 1, #Player.DisplayName do
			local Letter = string.sub(Player.DisplayName, i, i)
			local TextLabel = Instance.new("TextLabel", Nametag.Letters)
			table.insert(Labels, Count, TextLabel)
			TextLabel.AnchorPoint = Vector2.new(.5,0)
			TextLabel.Text = Letter
			TextLabel.Name = Letters[Count]
			TextLabel.BackgroundTransparency = 1
			TextLabel.Font = Enum.Font.GothamBold
			TextLabel.TextColor3 = ConvertColor3(Color)
			TextLabel.TextSize = 30
			Count += 1
		end

		Nametag.Subtext.TextLabel.Text = "@" .. Player.Name

		Nametag.Parent = Character.PrimaryPart

		for I, Label in pairs(Labels) do

			Label.Size = UDim2.fromScale(1/#Labels, 1)
			Label.Position = UDim2.fromScale(I/#Labels - (Label.Size.X.Scale/2), 0)
			wait()
			Label.Size = UDim2.new(((1/#Labels)/10)*Label.TextBounds.X,0,1,0)
			Label.TextScaled = true
		end


		delay(1, function()

			while Character do
				wait(5)
				NametagData[NametagStyle].Timed(Labels)
			end
		end)
	end

Now, this works perfectly fine for usernames such as mine:
image

But for certain other usernames, the spacing is very off:
image

Does anyone have a fix for this? Thanks!

You weren’t taking into account that the letters are a different size when centering. I mean you are a little, but not enough.

You need a way to determine the distance of each character from the center of the total size of the string when all textLabel sizes are added together. I would have a table I can go through that holds all of the positions of each character starting at 0, Then I subtract half of the fullStringSize from each of those giving me the position relative to the center of the fullStringSize. (I would recommend still using offset and converting offset to scale at the end). Then you can just add the relative position of the character to the center of the parent. (I can’t get this to read well… hopefully the psuedocode below will help you grasp what I mean)

FullStringSize is made by adding Label.TextBounds.X from all labels together to get the full actual size of the string.

Something like this

for index, char in characters:
  DistanceFromCenterOfFullString = characterPositions[index] - FullStringSize --Keep in mind you have to add all of the text bounds together to get the fullStringSize
  char.Position = UDim2.fromOffset(char.Parent.AbsoluteSize.X/2 + DistanceFromCenterOfFullString, 0)
  char.Position = UDim2.fromScale(char.Position.X / char.Parent.AbsoluteSize, 0) --Converts offset to scale.

If something seems off about my response it’s probably because most of it is a copy from Issues with TextSizing - #4 by oskxzr

1 Like

You can try using list layouts or by looping through it text label and scaling to size. I would also not use text scaled since that can screw up the letter size so just setting them to like 40 should do.

You could technically use a UIListLayout if you put the TextLabel inside of another frame. That way you can tween the TextLabel itself, while the frame’s position is in the UIListLayout.

Where before you might be doing this:
image

You would do this instead:
image

@tlr22’s method is probably better, but a little more complicated. Either solution should work though.

1 Like

:flushed:

In the script I’m setting the size of the text label to it’s TextBounds with some arithmetic performed on it and then setting it to textscaled, so I don’t have the issue of different sizes.

Oops! Forgot I made that. Thanks for your solution.

Why didn’t I think of that- I’ve literally done it before.

1 Like