newTextBox automatic width and height

Hello guys,

can anyone tell, if there is a way to deactivate scrolling for newTextBox, and let it adjust itself to its text width and height?

Or is there a way to get string width and height (also for multiline strings), which I then can use to dynamically resize a text box, like:


local text = "Hello\nWorld."

local stringWidth, stringHeight = text:getSize()

local box = native.newTextBox(event.x, event.y, stringWidth, stringHeight)?

-cheers

The problem with what you are trying to do is its very dependent on the font size and how the device renders the font.  You also need to define at least one of the box parameters too.  So if you know you’re font is 12 points, and you’re box is 240px wide, then you can guess and get close.  Most fonts characters + space between them will average out to about the font height, or perhaps 80% of the font height (depends on the font).  M’s and W’s take up a lot of space, I’s (uppercase i) and l’s (lower case L) take up very little space.  If you have a 500 char long string and a 12 point font and a 240px box then you can fit roughly 20 characters per line (240 / 12).  Then 500 chars / 20 per line = 25 lines.  25 lines * 12 points high + line spacing of roughly 50% would be more like 25 * 18 or around 450 px high. 

So no, we don’t have a way to do that calculation. 

thank you for the hint and the kind answer

The problem with what you are trying to do is its very dependent on the font size and how the device renders the font.  You also need to define at least one of the box parameters too.  So if you know you’re font is 12 points, and you’re box is 240px wide, then you can guess and get close.  Most fonts characters + space between them will average out to about the font height, or perhaps 80% of the font height (depends on the font).  M’s and W’s take up a lot of space, I’s (uppercase i) and l’s (lower case L) take up very little space.  If you have a 500 char long string and a 12 point font and a 240px box then you can fit roughly 20 characters per line (240 / 12).  Then 500 chars / 20 per line = 25 lines.  25 lines * 12 points high + line spacing of roughly 50% would be more like 25 * 18 or around 450 px high. 

So no, we don’t have a way to do that calculation. 

thank you for the hint and the kind answer