Autoamtically adjusting the size of a GUI element according to the amount of text

I am trying to make a log that shows all of the recent kills in the game. It will go in the top-right corner and show the killer’s name on the left, an icon to represent the used method in the killing, and then the victim’s name on the right. I am, however, running into problems with appropriately sizing the names of the player and ultimately the entire frame. I want the frame to shrink down in size to match the lengths of the players’ names while still having the image representing the method between the killer’s name and the victim’s name. I have made countless attempts for hours to figure out how to get this to work, although I believe it is best done using UIListLayout as a helper. That being said though, it is still not working correctly and the sizing is never right. This is the code I have as of present to manage the size:

local Adjust = {}

local TextService = game:GetService("TextService")

function Adjust.resize(activity, killerName, victimName)
	local killerLabel = activity.Killer
	local victimLabel = activity.Victim
	local image = activity.MethodImage

	local SIZE_PER_CHAR = #killerLabel.Text / killerLabel.Size.X.Scale

	local DEFAULT_SIZE = killerLabel.Size.X.Scale

	killerLabel.Text = killerName
	victimLabel.Text = victimName

	-- figure out required length
	local killerNeeds = #killerLabel.Text * SIZE_PER_CHAR
	local victimNeeds = #victimLabel.Text * SIZE_PER_CHAR
	
	local SIZE_DOWN = killerNeeds + victimNeeds / DEFAULT_SIZE
	
	-- fixed layout budget (stable UI rule)
	local IMAGE_SCALE = image.Size.X.Scale / SIZE_DOWN
	local TEXT_BUDGET = 1 - IMAGE_SCALE

	-- weights
	local k = #killerName
	local v = #victimName
	local total = math.max(k + v, 1)

	local kShare = k / total
	local vShare = v / total

	-- apply sizes
	image.Size = UDim2.fromScale(IMAGE_SCALE, 1)

	killerLabel.Size = UDim2.fromScale(TEXT_BUDGET * kShare, 1)
	victimLabel.Size = UDim2.fromScale(TEXT_BUDGET * vShare, 1)
	
	-- resize the frame to fit the text
	
	local TOTAL_SCALE = killerLabel.Size.X.Scale * SIZE_DOWN
	
	activity.Size = UDim2.fromScale(TOTAL_SCALE, activity.Size.Y.Scale)
end

return Adjust

Note that I am using TextScaled for both text labels and that there is no padding in the UIListLayout. This is what the UI element looks like when tested with killerName = “10_letters” and victimName = “5_let” as inputs:

image

Note that the size of the scope, MethodImage.Size.X.Scale = 0, which is why “5_let” appears partailly off screen.

That being said, how can I fix my formula such taht names appear in the right size and that the frame appropriately sizes down to match the need for space? In one of my previous attempts, I tried using GetTextBoundsAsync but I was unsure of it’s effectiveness with the TextScaled property being enabled so I made another attempt.

This is what the GUI elements should look like in the end, note the element in red is not perfectly sized, but is much closer to the actual goal:

1 Like

Hey! I believe I managed to do what you are looking for.
I have a main container frame that sets the height for every “message”
This one has an UIListLayout along with the message frames. These have a Size of 0, 0, 1, 0 and an AutomaticSize of X
In every message frame, there is another UIListLayout with HoritzontalAlignment set to right
In every message frame, there is also Username_1 and Username_2 textlabels. These have TextScaled enabled, a Size of 0, 0, 1, 0 and an AutomaticSize of X

I’ll attach an image and the place file in case I didn’t explain myself correctly


DevForumFile.rbxl (66.4 KB)

Let me know if this was helpful!

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.