Jump to content
  • 6

API: Add a way to calculate text width & height before rendering/assigning it


Posted (edited)

It would be great to have a way of knowing text width & height (height is more important actually) before rendering it or creating an element for it.

-- function getTextDimensions(text, fontSize, maxWidth, fontType, isBold, isItalic, isOutlined, isShadowed)
-- Arguments:
-- string text
-- int fontSize
-- int maxWidth (maximum text width, after exceeding it text should use new lines)
-- FontType fontType (optional, default: FontType.Normal)
-- bool isBold (optional, default: false)
-- bool isItalic (optional, default: false)
-- bool isOutlined (optional, default: false)
-- bool isShadowed (optional, default: false)

local w, h = getTextDimensions("The \\c(0d0)hideout\\c() is located in \\s(45:15)", 15, 400)

Or maybe it would be easier to implement "textWidth" and "textHeight" read-only properties for TextField and Label elements that would tell the actual width and height of their assigned text, so we could instantly resize it after creating.


Here is an example. Let's say we have multiple rows with various text in a ScrollFrame. If we're using static pre-determined row heights, we will get empty spaces after text or text that goes out of the designated rectangle. Even with auto-resize (shrinking), huge amount of text would look ugly, being too small and unreadable (also auto-shrinking doesn't work with multi-line labels). Having a way to know the actual height of the text would allow to create rows that would fit the text no matter what.


Why not just create this function in Lua?

  1. Because we would have to manually extract relative width for every character in Unicode font (TTF fonts support up to 65535 characters ) for every font (not just vanilla, but the modded ones too!)
  2. And because we would have to re-implement the Unicode Line Breaking Algorithm in Lua (which would have low performance)
Edited by Rinart73
more reasons why this should be done

Share this post

Link to post
Share on other sites

0 answers to this suggestion

Recommended Posts

There have been no answers to this suggestion yet

Answer this suggestion...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...