Problem with text and newline \n

Hi, I noticed a sort of bug with the following code:

local txt=display.newText(“HELLO\nWORLD”,150,150,nil,20)

HELLO and WORLD stay on the same line.

I have this problem on Mac Simulator (build 2015.2590). I don’t know if it works on the device, somebody could try it?

Thanks for your help!

PS: It works perfectly on Windows Simulator

Newlines are only processed if you’re using display.newText() in multi-line mode.  You need to specify a width and height so Corona know’s where to wrap the text (and process newlines), perhaps something like:

local txt=display.newText(“HELLO\nWORLD”, 150, 150, 300, 0, nil, 20 )

Where 300 is defining a 300 pixel width for the text and 0 says use as much height as needed…

Rob

Thanks but I know this… The problem is a little more complicate:

I must not specify width and height because I will use the generated dimensions of the text to fit a kind of rectangle around it.

If I write 300 for the width, that rectangle will always be 300 even is the text is short. It makes the thing impossible.

txt=display.newText(“HELLO \n WORLD”,150,150, 0, 0 , nil,20) – txt.width=80 ,txt.height=47

With that I can add a perfect rectangle around this text.

On Windows it works very good, why not on Mac ?

The reason?  Because they are different operating systems.  iOS and Android will likely not work like you’re wanting.

Rob

Well, so I will try find a crazy workaround for it :slight_smile:

Thanks for your help and fast answer :wink:

All my text shows up funny (too small, wrong line etc) on mac simulator, but works fine on iOS device. You can try yours on device, most probably works fine too.

But do note the width and height of the text box will be slightly different on diff devices due to the cropping of text.

Bug ID: 40035

Rob

Newlines are only processed if you’re using display.newText() in multi-line mode.  You need to specify a width and height so Corona know’s where to wrap the text (and process newlines), perhaps something like:

local txt=display.newText(“HELLO\nWORLD”, 150, 150, 300, 0, nil, 20 )

Where 300 is defining a 300 pixel width for the text and 0 says use as much height as needed…

Rob

Thanks but I know this… The problem is a little more complicate:

I must not specify width and height because I will use the generated dimensions of the text to fit a kind of rectangle around it.

If I write 300 for the width, that rectangle will always be 300 even is the text is short. It makes the thing impossible.

txt=display.newText(“HELLO \n WORLD”,150,150, 0, 0 , nil,20) – txt.width=80 ,txt.height=47

With that I can add a perfect rectangle around this text.

On Windows it works very good, why not on Mac ?

The reason?  Because they are different operating systems.  iOS and Android will likely not work like you’re wanting.

Rob

Well, so I will try find a crazy workaround for it :slight_smile:

Thanks for your help and fast answer :wink:

All my text shows up funny (too small, wrong line etc) on mac simulator, but works fine on iOS device. You can try yours on device, most probably works fine too.

But do note the width and height of the text box will be slightly different on diff devices due to the cropping of text.

Bug ID: 40035

Rob