Can't Change Wrapping Width of display.newText( ) Object

Hi,

I’m not exactly new to Corona, but I came across a seemingly simple problem, the solution to which I cannot find. Thus I seek help in the Corona community.

The display.newText( ) can be created the simply way (with only x, y, and the message), or the more complex way (with a table of parameters). Today, I would like to talk about the latter.

Suppose we have a text object: 

myText = display.newText { text = "My Very Very Long Text", x = 200, y = 200, width = 100, font = native.systemFont, fontSize = 20, align = "left" } 

Now we can move the location of the text after creation by setting myText.x = 100

We can even change the font size of the after creation by setting myText.size = 30

My question is: is there any way to change the wrapping (and not literal) width of a text object AFTER creation?

I tried changing the “.width” property, but instead of changing the wrapping width of text object, I just cut off the message on both ends, which clearly is not my desired result.

I’ve attached a photo to better illustrate what I mean.

Any help is appreciated. 

Thank you in advance!
KC

Have you tried setting the width then updating the .text with the new string?

That should cause the text object to regenerate.

Rob

@Rob Miracle

I tried what you described, and no, it did NOT cause the text object to regenerate. In fact, it had no effect whatsoever as far as I can tell.

Surely there has to be a simple solution to such a simple problem??

KC

Did you update with the same string or a different one?

I had this exact thought in mind. 

First I tried it with the same string, as in myText.text = myText.text, but that didn’t work.

So I tried it with a different string, as in
local temp = myText.text…" "
myText.width = 80
myText.text = temp

but unfortunately that didn’t work either… :frowning:

Humor me and try:

local temp = myText.text.." " myText.text = "" myText.width = 80 myText.text = temp

We likely have a check to say if the assigned text is the same as what you are trying to resign, then don’t update. But by making the string “change” it should force the text object to regenerate.

Rob

I tried your code, and something interesting happened:

It did force the text object to regenerate, but to the wrong width!

Instead of forcing it to regenerate to the new, intended width, I just went right back to the old, original width.

(In my little example, this would mean the new width would be 100, the same as the original, instead of the intended 80)

FYI, remember text is just a bitmap object.  Easy fix - just drop  myText  and recreate on a text change.  

I assume that behind the scenes that is what Corona should be doing anyway but if Corona is trimming the string then adding a space might make no difference.

What exactly do you mean by “drop” the ‘myText’ object? Do you mean destroying it and creating a new one from scratch?

Yes, drop and recreate on a change of width.  It will be the same cost as telling Corona to change the text anyway.

It will sort your problems instantly.

@SGS

That worked, but it doesn’t really answering the question “how to change the width of a text object after creating it”.

I tried collectgarbage("count) and, contrary to what you said, it is much more expensive than say changing the .x or .size property of a text object. (makes sense since you are creating an entirely new object). Moreover, it takes up many more lines of code than should be necessary, and it is just god damn ugly code.

I am both extremely shocked and disappointed to learn that there is no way to do this like the way you would change the location or font size.

There is no excuse for the Corona team not to include such simple and obvious functionality.

They should include this in a future build, or if not at the very least, should mention this in the gotcha clause in https://docs.coronalabs.com/api/library/display/newText.html

I only try and help with solutions to problems… I cannot address the problems as I am not Corona.

@SGS
My criticism was not aimed at you. I was trying to reach out to the Corona team

Rendering text is an expensive operation. We have to take your string, pass various font information to the system’s API (which is different for each OS) and create a texture and a mask and return that rectangle back to OpenGL as a graphic texture.

When you change .text it should force the whole rendering process to fire. Replacing the text with the same text likely has a safety to prevent the text from re-rendering, which is why I suggested what I did above.

Still, the problem is the width. Since your working with an image in OpenGL changing .width probably just changes the texture width and doesn’t trigger a re-rendering. The width you pass in is a parameter and it doesn’t actually represent the width of the object, just the value we tell the OS text renderer to return.

That’s why I was hoping changing the text would force a re-render.

For us to address this we need a small demo project that we can run and see the problem. This includes a main.lua, config.lua, build.settings any font’s you’re using that’s not standard, and any other assets what we can unzip, point the simulator to it  and see the problem (or build for device and see the problem.

Compress this project into a .zip file (please try and avoid other compressions, like RAR) and fill out a bug report here:

https://portal.coronalabs.com/bug-submission

Thanks

Rob

@Rob Miracle

Ok, I will find the time to do so. Thanks for responding.

Have you tried setting the width then updating the .text with the new string?

That should cause the text object to regenerate.

Rob

@Rob Miracle

I tried what you described, and no, it did NOT cause the text object to regenerate. In fact, it had no effect whatsoever as far as I can tell.

Surely there has to be a simple solution to such a simple problem??

KC

Did you update with the same string or a different one?

I had this exact thought in mind. 

First I tried it with the same string, as in myText.text = myText.text, but that didn’t work.

So I tried it with a different string, as in
local temp = myText.text…" "
myText.width = 80
myText.text = temp

but unfortunately that didn’t work either… :frowning:

Humor me and try:

local temp = myText.text.." " myText.text = "" myText.width = 80 myText.text = temp

We likely have a check to say if the assigned text is the same as what you are trying to resign, then don’t update. But by making the string “change” it should force the text object to regenerate.

Rob