Trying to make a little tip appear giving you some more information when hovering over parts in workspace. It works well, with the exception of the TextLabel looking, eh…
I know I read something about an upcoming feature on scaling TextLabels based on their text’s length, and I am not if it released or not.
And if it didn’t, what other alternatives could I use? Are there any known algorithms for calculating sizes like these? I was never able to make a proper one in the past. Font is Code so this should help very much.
I know that’s a thing, but I’m looking into ways to scale the entire TextLabel relative to the text’s length. I think it would look way better this way. It’s still a last resort option though.
You could look into using UITextConstraint with textscaled and it should look better. If not then you can create a script for this.
function TextSize(Frame, Percent)
local Size = Frame.AbsoluteSize.Y * (Percent/100)
if Size > Frame.AbsoluteSize.X then
Size = Frame.AbsoluteSize.X * (Percent/100)
end
return Size
end
Managed to do an algorithm without any sort of help.
If anyone needs it:
local function calculateSize(String)
local Lines = math.ceil(string.len(String) / 30) -- / 30 should be replaced with the max characters on your line.
Tip.Size = UDim2.fromOffset(210, Lines * 14) -- * 14 should be replaced with however big you want a line to be
-- Also Replace Tip to whatever TextLabel you're changing the size of.
end
In general I only recommend fonts like Code, where all characters have equal width.
I know this is an old post but I recently came across a new and easier way of doing this. You can use the TextBounds property to get the area of the text’s size of the textlabel in offsets. You can then resize the label using these offsets to have it so that the frame will only be as big as the text. No calculations needed.
If you would to read more into TextBounds or any other property, here is the documentation for the TextLabel : Link
Sorry for the late reply, I understand that you had the solution but alternatively, you may also enable the “TextScaled” property on your text label that you want to autoscale.
Yes, but the text size gets smaller for the player. I am also looking to do the same thing where the TextLabel scales itself to keep the text size the same no matter how many characters there are. It will just keep getting bigger on the y-axis the more text is added.
For people who might need an alternative method, here’s how I did it:
local textsize = math.ceil(Label.TextBounds.Y)
Label.TextScaled = false
Label.TextSize = textsize
local function calulate_lines(text)
text = string.gsub(text, "[\r\n\t]", " ")
Label.Visible = false
local lines = 1
local currenttext = ""
local start = true
for i, word in string.split(text," ") do
if start then
start = false
currenttext = word
else
currenttext = currenttext.." "..word
end
Label.Text = currenttext
if not Label.TextFits then
lines += 1
currenttext = ""
start = true
end
end
Label.Text = ""
Label.Visible = true
return lines
end
Have your text label scaled to the size of a single line, then multiply the scale by the amount of lines returned by the function and this works perfectly. This works regardless of if the font is fixed width or not.