Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
21

How would I recognize if Text reaches the AbsoluteSize of a TextBox? [closed]

Asked by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 4 years ago

Hello!

I'm attempting to rebirth an old custom chat project! I've come quite far and am now just implementing a few extra bells & whistles.

I'm having an issue with the latest addition, though. I am trying to integrate the default chat's feature of appropriately resizing the TextLabel on the Y-Axis to accommodate for text-overflow.

I thought for a minute and came up with the necessary steps required to achieve my goal, except I'm now having issues with properly executing my theory. I know that I must determine when the text exceeds the TextBox's AbsoluteSize on the X-Axis, and can recognize this with TextService's :GetTextSize() function, yet, it's not working the way I intended:/

What am I doing wrong?

local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local Chat = PlayerGui:WaitForChild("Chat")
local ChatFrame = Chat:WaitForChild("ChatFrame")
local ChatBar = ChatFrame:WaitForChild("ChatBar")
local TextService = game:GetService("TextService")

ChatBar:GetPropertyChangedSignal("Text"):Connect(function()
    local TextTo = TextService:GetTextSize(ChatBar.Text, 8, "Code",
        Vector2.new(ChatBar.AbsoluteSize.X, ChatBar.AbsoluteSize.Y)
    )
    if (TextTo.X > ChatBar.AbsoluteSize.X) then
        local CurrentSizeY = ChatBar.Size.Offset.Y
        ChatBar:TweenSize(UDim2.new(1, -10,0, CurrentSizeY + 15), "Out", "Sine", 1)
    end
end)

Thanks!

Locked by EmbeddedHorror and hiimgoodpack

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
5
Answered by
clc02 553 Moderation Voter
5 years ago
Edited 5 years ago

What's happening is you're trying to see how big a piece of text will be inside a given frame, and if it goes beyond the size of the frame you'll make a new line.

The problem is the text size will never be any bigger than the frame you're testing it against, as it'll just not put the text up if it extends past the limit of the frame, giving the frame's size instead.

For example: print(game:GetService("TextService"):GetTextSize( "test" ,8 , "Code" , Vector2.new(10, 10))) and print(game:GetService("TextService"):GetTextSize( "testtesttest" ,8 , "Code" , Vector2.new(10, 10)))

will both return {10, 8}, because the overall size of the text can't exceed the size of the frame, which is 10, 10.

The check you're doing is to equivalent to seeing if n is > 10, where n is always less than or equal to ten. There is no overlap, and thus won't create a new line.

To bypass this, you need to make the frame slightly wider than it actually is, you do this by changing line ten to Vector2.new(ChatBar.AbsoluteSize.X+10, ChatBar.AbsoluteSize.Y) Which adds an extra ten pixels to the width. This lets your next check to see if it's bigger than the absolute size of the frame succeed, as the width is allowed to be that + 10 if there's enough characters.

edit: For those who come after, the problem was that TextSize and FontSize have a conversion table on the wiki. You no longer need to convert, if the TextSize is 15, you need to use 15 as the second argument in GetTextSize

0
I'm slightly confused, maybe it's just the Script doing something weird  Ziffixture 6913 — 5y
0
I'll try to rewrite it. clc02 553 — 5y
0
If you go the route of putting line breaks, it might be useful that a character takes five pixels in the font/size you used, so if your width is 100, you'd need to insert "\n" after every 20 characters clc02 553 — 5y
0
Thanks for the help again @clc02! Ziffixture 6913 — 5y
Ad