How to Align multiline text?

It is strange that Corona SDK seems doesn’t support text alignment properly !!please correct me if am wrong!

what I wanted is very simple

I want multiline text which is aligned to the center or right?
I tried all the following objects, nothing worked for me

display.newText

display.newRetinaText

native.newTextBox

any help will be appreciated! [import]uid: 8519 topic_id: 24738 reply_id: 324738[/import]

Hi,
Instead of the display.newText you can use display.newParagraph.
I hope it’ll help you. [import]uid: 75428 topic_id: 24738 reply_id: 100238[/import]

With the latest display.newText object, I always do something like this to ensure I get the text object aligned and placed exactly where I want it:

[lua]local myTxt = display.newText(localGroup, “Very long, long, long,\nlong, long, long, long\nsentence I want to wrap.”, 0, 0, screenW*0.8, screenH*0.8, fontName, fontSize );
myTxt:setTextColor(255, 255, 255);
myTxt:setReferencePoint( display.TopLeftReferencePoint );
myTxt.x = screenW*0.25;
myTxt.y = 100;[/lua]

The key is to setReferencePoint AFTER the content of the text is defined, and then set the x-position AFTER the setReferencePoint. y-position probably does not need to be reset after the text & reference point are defined (unless your y-position isn’t at the upper/top of the sentence). For proper alignment, it’s always a good idea to set x-position after the text and reference point are set.

Keep in mind, if you update the text of the same object, you will need to re-set reference point and x-position. (This one is easy to forget and can cause grief.)

Naomi

[import]uid: 67217 topic_id: 24738 reply_id: 100350[/import]

I have a similar problem trying to center multiple lines of text.

Using object:setReferencePoint the invisible “text box” is centered just fine, which means the longest line of code is centered on screen. But my problem isn’t about positioning the text box, its centering the actual text within the text box.

So it looks like:
first line
this is the second line

Again, the textbox itself is centered, but not the text inside it. Any help would be much appreciated! Thanks! [import]uid: 94309 topic_id: 24738 reply_id: 104093[/import]

I’ve been having the same problem for weeks but haven’t received a reply back from anyone at Ansca. [import]uid: 89562 topic_id: 24738 reply_id: 107311[/import]

Basically what people are after is centering the whole ‘block’ of multiline text, not simply centering the multiline text to the screen.
Using setReferencePoint before, after or whenever is not going to help center your whole ‘block’ of multiline text, the lines within the ‘block’ will continue to be left-aligned (even if the multiline ‘block’ ends up being centered to the screen).
Can’t believe something so simple has not been resolved yet.
And if corona ever gets round to it, adding in a line spacing variable would be most helpful too as the line spacing in multiline text is ridiculous. [import]uid: 66228 topic_id: 24738 reply_id: 140428[/import]

Basically what people are after is centering the whole ‘block’ of multiline text, not simply centering the multiline text to the screen.
Using setReferencePoint before, after or whenever is not going to help center your whole ‘block’ of multiline text, the lines within the ‘block’ will continue to be left-aligned (even if the multiline ‘block’ ends up being centered to the screen).
Can’t believe something so simple has not been resolved yet.
And if corona ever gets round to it, adding in a line spacing variable would be most helpful too as the line spacing in multiline text is ridiculous. [import]uid: 66228 topic_id: 24738 reply_id: 140428[/import]

+1 for variable line spacing and having more control over left / centred / right aligned multi-line text.

Hi guys,

I nearly finished a module that allows you to adjust your text as you wish. It includes the following things.

  • multilined text, aligned to left right and center

  • set your own linehight, textspacing, kerning

  • use <span> tags (like in html) to give certain text passages different properties (color, font size and underline)

It take a little bit more memory and little more time to create the textbox, but it worth it.

It’s nearly finished, but I have to do a few bugfixes and a documentation, because you have to set up a font before you can use it, and thats a bit more complicated as normal.

Sounds amazing!

fontbox-logo-blog.png

Hey guys, it’s finally done. This is the solution for all your problems :wink:

I finished my work at a module called fontBOX, that is available at my blog.

With this module you can create text fields, which offer you several formating options.

These are:

  • Manual line breaks
  • Manual line height
  • Left justified, right justified or centered text alignment
  • Individual passages can be recolored, resized or underlined
  • Write the entire text in uppercases
  • Kerning and letter spacing

Download it here >

Here some examples what could be done with this module.

examples.jpg

Hope you enjoy it :slight_smile:

Great! Will try right away. Thanks much for sharing your solution. Most appreciated. 

wow, wow and wow. I’ll check this out very soon! you rock.

It is amazing. Works wonderfully. Thank you very much for making it available. 

You are welcome my friend :wink:

I’m glad it works for other people as well as it does for me.

Please report me any bugs or issues you are running into.

Looking great torbenratzlaff - this is a lifesaver!

Thanks,

Nathan.

I noticed that the load time for my app is much longer once I call fontbox.newFontSet (see full call below). A few extra seconds in the simulator, but considerable on the device. Is there a way to manage this more efficiently, or a way to pre-load a font?

Thanks,

Nathan.

fontbox.newFontSet{&nbsp;&nbsp;&nbsp;&nbsp;name = "nativeBold",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font\_name = native.systemFontBold,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kerning\_pairs = {["a"]={["b"]=1,},},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; all\_letters = "abcdefghijklmnopqrstuvwxyzäöüÄÖÜßABCDEFGHIJKLMNOPQRSTUVWXYZ .,?!-:;\_+/()&%0123456789$'",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spacing = -0.01, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;

Hey Nathan,

thanks for responding.

I think I fixed the performance problem. Did some really unnecessary stuff there.

So the 1.1 version is online now.

Download >

Would like to know if it runs faster now :slight_smile:

No noticeable difference - still 5+ seconds to load in the simulator and 10+ seconds on my iPhone.

Nathan.

Wow, ok, that’s strange.

Because after the change I did, I got no loadtime on simulator and around 1 sec on phone.

To explain what happens when you call fontbox.newFontSet:

To position all letters correct I have to get relation between their width and the fontsize. For that I create a textbox and loop through all characters an devide the textbox’s width with the fontsize. The only thing that I could do are funtions that let you save and load fontsets. With that you can save the fontset and load it at the next start.

One questions, did you use fontbox.addSpecialCharacters BEFORE you call fontbox.newFontSet ?

Otherwise I have no idea what could be done against the loading problem, because I couldn’t reproduce it :frowning: