Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
15

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

Asked by
Feahren 1586 Moderation Voter
5 months ago
Edited 5 months ago

Hello. I'm trying to recreate my severely broken Chat, I've come quite far, and I'm just implementing some extra bells and whistles to the Chat itself. I'm having issues with one of them, I'm not sure if this is the proper formula to be using, or proper use of it. Basically, I'm trying to recognize if the current Text of whatever Is resting in the TextBox, has reached the AbsoluteSize.X of the GUIObject. I'm using TextService to attempt this, but it doesn't seem to be working properly, how could I do this? 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!

0
welp, 7 upvotes! thats a worldrecord!! here have an 8th ;) starmaq 737 — 5mo
0
make it 9 gioni01 2040 — 5mo
0
Holy! Thanks guys! Feahren 1586 — 5mo
0
Here, have an 11th! CaptainD_veloper 269 — 5mo
View all comments (6 more)
0
congratulations ur front page on best gioni01 2040 — 5mo
7
Why does this have so many upvotes. This isn't a bad question but i've seen better. incapaxx 3366 — 5mo
0
Maybe it's because of the recent lack of good, ordinary questions. Honestly, this question shouldn't have this many upvotes, but it doesn't really surprise me that it does. User#25115 0 — 5mo
0
It doesn’t matter if I’m getting upvoted, though I am very thankful:), there is no reason to get mad or discriminate Feahren 1586 — 5mo
0
I'm not discriminating. It's a good question. I was just speculating as to the cause of all these upvotes. User#25115 0 — 5mo
0
Fair argument, I take it back Feahren 1586 — 5mo

1 answer

Log in to vote
5
Answered by
clc02 553 Moderation Voter
5 months ago
Edited 5 months 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  Feahren 1586 — 5mo
0
I'll try to rewrite it. clc02 553 — 5mo
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 — 5mo
0
Thanks for the help again @clc02! Feahren 1586 — 5mo
Ad

Answer this question