Getting the real width of text in a text field.

Hey there !

I’ve got a text field which needs to be multiline and centered. That means I have to set a width. That works fine.

local text = display.newText({text="Hello World", fontSize=12, x=display.contentWidth\*0.5, y=display.contentHeight\*0.5, width=640, align="center", font="Arial"})

If I print the text width, it will return “640”, which corresponds to the width of the text area and not the actual text.

But is there any way to get the real width of “Hello World” ?

Hi @evanspro,

May I ask what your usage need is for getting that exact width? I don’t think there’s a direct way… most likely you would need to create a separate text object of the same string, but not multi-line and no set width, and then gather its width and store it to a variable. It’s a fairly backwards workaround, yes, which is why I’m curious why you need this value and what you intend to do with it.

Best regards,

Brent

evanspro: Yep, from memory you’ll have to create it with no set width and then extract it. It is odd that in 2015 Corona SDK does not have a built-in way to create multiline text with options for justification, width, and leading but, well, it’s an imperfect universe.

Hi @Brent and @corona273 !

I’m trying to underline a text, just like that :

helloworld.png

The “problem” is that the text is dynamic, so I can’t know what’s gonna be its “real” width…

There isn’t any way to “autosize” (like in actionscript) the text field once the text has been loaded which would allow me to get the final width ?

I thought about separating the different lines into differents text fields, then get their respectives width, which seems like a lot of work for what could be done “easily”.

Guess it’s time to get to work then ! :slight_smile:

I believe, if you set up a custom font for the text field and use the same font and font size to make a text object (display.newText()), they will have the same width.  

So, while it is hacky, you could make an offscreen display.newText() with the same text as the text field, you could then measure its contentWidth.

And to adjust the line spacing!

And to adjust the line spacing!

Alan: That’s what I meant by leading (interline spacing). :slight_smile:

Omissions like this really – sadly – limit Corona’s broader appeal. You can do text formatting like this more easily 20 years ago in HTML than in Corona today.

Hi @evanspro,

May I ask what your usage need is for getting that exact width? I don’t think there’s a direct way… most likely you would need to create a separate text object of the same string, but not multi-line and no set width, and then gather its width and store it to a variable. It’s a fairly backwards workaround, yes, which is why I’m curious why you need this value and what you intend to do with it.

Best regards,

Brent

evanspro: Yep, from memory you’ll have to create it with no set width and then extract it. It is odd that in 2015 Corona SDK does not have a built-in way to create multiline text with options for justification, width, and leading but, well, it’s an imperfect universe.

Hi @Brent and @corona273 !

I’m trying to underline a text, just like that :

helloworld.png

The “problem” is that the text is dynamic, so I can’t know what’s gonna be its “real” width…

There isn’t any way to “autosize” (like in actionscript) the text field once the text has been loaded which would allow me to get the final width ?

I thought about separating the different lines into differents text fields, then get their respectives width, which seems like a lot of work for what could be done “easily”.

Guess it’s time to get to work then ! :slight_smile:

I believe, if you set up a custom font for the text field and use the same font and font size to make a text object (display.newText()), they will have the same width.  

So, while it is hacky, you could make an offscreen display.newText() with the same text as the text field, you could then measure its contentWidth.

And to adjust the line spacing!

And to adjust the line spacing!

Alan: That’s what I meant by leading (interline spacing). :slight_smile:

Omissions like this really – sadly – limit Corona’s broader appeal. You can do text formatting like this more easily 20 years ago in HTML than in Corona today.