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

How do I get a string's size with a specified font?

Asked by 8 years ago

I want to scale a GUI to the text it contains, but I want the font to remain consistent across GUIs, no matter the length of content. I come from GMod, where there's the function http://wiki.garrysmod.com/page/surface/GetTextSize, which returns the text width and height for the specified font size, but I couldn't find anything similar. Is there a function like this for ROBLOX, or a property of a TextLabel I can use to determine this, or am I best just guessing at a formula for it?

3 answers

Log in to vote
1
Answered by 8 years ago

I actually made a module for this.

http://www.roblox.com/TextBounds-Module-item?id=383144025

local getbounds = require(383144025)

function GetStringWidth(str, size)
    local strtbl        = {}
    local width = 0
    for i = 1,#str do
        table.insert(strtbl, str:sub(i, i))
    end
    for _,char in next, strtbl do
        width = width + getbounds[size][char]
    end
    return width
end

print(GetStringWidth("Hello, world!", "Size9"))

Here's a function I wrote for you. Hope I helped. Replace "Size9" with whatever size font you are working with.

Ad
Log in to vote
0
Answered by 8 years ago

Unfortunately, ROBLOX doesn't really provide anything like that. Some of the following things may be useful for you, though.

string.len()

Returns the length of a string. If you are wanting to do some gui scaling, make an example of text, then scale according to that, like the second code block inside this header.

Here is how you call it:

string.len("Hello world!")

Example of scaling:

local maxLength = 10 -- 10 characters
local gui  = script.Parent -- a textlabel or something else with text in it
local length = string.len(script.Parent.Text)

 -- let's say 1 is the base size of the text object, it can fit 10 characters. But, we don't know how long the text will be, because it's .. (a player's name/map name/etc.), so we want to resize it according to the text's length

gui.Size = UDim2.new(1/maxLength*length,0,1,0)

Font sizes

Font sizes are based off of pixels in ROBLOX, so you could scale the guis accordingly with offset (the pixel version of scaling of gui, and their default). You check the font size, the resize the gui, probably using some of the previous header.

For example:

local gui = script.Parent
if gui.FontSize == Enum.FontSize["32px"] then
    gui.Size = UDim2.new(0,(32+5)*string.len(gui.Text),0,100)
end

-- (32+5) = 27
-- multiply it by the length of the text (I'm assuming each character is 32px wide/tall?)

Text scaled

All text objects in ROBLOX have a bool property called 'TextScaled'. When it is set to true, the text is automatically scaled so it fits the gui. Probably one of the least satisfactory things in this list, though. It just doesn't look as good as manually sized text.

Hopefully this helped :) Tell me if you need any more help in the comments.

~TDP

0
The characters are all the same height with the font size, but the width varies with the kerning. I guess I can find the widest character (Likely M), and then assume every character is like that and leave trailing. Thanks. GhostSailor 5 — 8y
0
You're wrong. Absolutely wrong. TextLabel.TextBounds.X and .Y provide the width and height in pixels. 0xDEADC0DE 310 — 8y
Log in to vote
0
Answered by 7 years ago

Hi! I understand this is a quite late response to your question, but it seems you finally have a proper answer to your question!

TextService/GetTextSize

This property allows you to get the text size from a specified string, font and font size. All you have to do is:

local TextService = game:GetService("TextService")

print(TextService:GetTextSize(
    "This is the text I'm using",
    24, --/ This is the font size I want
    Enum.Font.Arial, --/ This is the font you're using
    Vector2.new(10000, 10000) --/ This is the maximum size in pixels of the text frame.
)) --> Returns: 234, 24

Note that I set the Vector2 to a large number so that the text is not affected by the TextScaled property of a GUI. Do not attempt to set it to math.huge, as the TextService cannot cope with this and will return inaccurate results.

Answer this question