How can I determine the maximum font size for a display.newText with a given string and font that will be under the maximum texture size limit?

Hi Corona!

For a given string and font I would like to set the highest font size that I can that will keep the texture under the maximum texture size for any given device.

Is there anyway to determine the resultant text object size without actually creating a new text object with trial and error values?

Thanks

Well if you get max texture size, you can divide that by your string length to get a rough idea of the maximum font size.   A 20 point font is roughly 20 pixels high (in content area points)  There will be some vertical padding to cover descenders and such.  An M is roughly the same width and height, but you have space between characters, but by the time you average out the M’s and the i’s using the font height as the average character width is pretty close.

So if your max texture size is 2048 and you have a 100 character line of text, your max font size would be around 20 points.

Rob

Thanks for your response Rob.

I ended up just doing a trial and error:

  1. Create text object with specific font size and save the width. Delete the text object.

  2. Compare that width with the previous created width. If it is the same then the text object cannot get any bigger and we found the max font size. Finish. Else goto step 3.

  3. Increment font size and goto step 1

There is a performance hit of course to be creating and deleting text objects, but its not a big deal in my use case.

Well if you get max texture size, you can divide that by your string length to get a rough idea of the maximum font size.   A 20 point font is roughly 20 pixels high (in content area points)  There will be some vertical padding to cover descenders and such.  An M is roughly the same width and height, but you have space between characters, but by the time you average out the M’s and the i’s using the font height as the average character width is pretty close.

So if your max texture size is 2048 and you have a 100 character line of text, your max font size would be around 20 points.

Rob

Thanks for your response Rob.

I ended up just doing a trial and error:

  1. Create text object with specific font size and save the width. Delete the text object.

  2. Compare that width with the previous created width. If it is the same then the text object cannot get any bigger and we found the max font size. Finish. Else goto step 3.

  3. Increment font size and goto step 1

There is a performance hit of course to be creating and deleting text objects, but its not a big deal in my use case.