FAQ IconIt’s Wednesday and time for another frequently asked questions (FAQs) session. Here are some FAQs about display text objects.

1. I’m creating lots of display.newText objects and it’s slowing down my app. Why?

It may seem like text is a simple thing to display on the screen but it can be very complicated and time consuming. When you create text with display.newText, the text must be first converted into a bit map and then sent to openGL so it can be rendered on the screen. The larger the text (in the number of character and font size), the longer this process takes. It also consumes texture memory just like displaying images.

2. I’m moving text around on the screen, will that slow down my app?

Moving or scaling text objects does not slow down Corona SDK. If you are changing the text, the bit map needs to be recreated, which can impact the frame rate. The same is true when changing the font size or the font type. These all require a new bit map and may affect performance.

3. I’m using display.newText and see a white or black block where the text should be. What’s wrong?

The black or white block is what you see when the text exceeds the texture limit of the device. Since text objects are converted to bit maps before being displayed, long text strings with a large font size can easily exceed the texture memory limit of some low-end devices. The solution is to limit the amount of text displayed in a single display.newText object. You should break up large text objects into multiple text objects.

4. How do I align text?

Text Justification
Starting with Daily Build 1143 we added text alignment to display.newText. You can specify “left“, “center” and “right” justification. The only limitation is you can’t change the justification after the text object has been constructed.

When we added text justification to display.newText, we changed the way parameters are supplied to the function. The parameters are now sent in a Lua “option” table instead of as individual parameters. display.newText still supports the legacy call, but you can’t use it to justify the text. Be aware that if you call display.newText with the new option table, you specify x and y values, which reference the center of the text object and not left and top used in the legacy call.

In order for text justification to work, you must supply the width parameter. If the height parameter is not supplied, it’s assumed to be 0 (this may change in the future). Left justification is assumed if no justification is specified.

display.setDefault( "background", 128 )

-- Create a rect to show the boundaries of our text fields
local rect = display.newRect( 50, 35, 200, 150 )
rect:setFillColor( 40 )

-- Right aligned, single line
local textOptions1 = { text = "Line 1 right aligned", x = 100, y = 50, width = 200, align = "right", font = native.systemFont, fontSize = 14 }
local text1 = display.newText( textOptions1 )
text1:setReferencePoint( display.TopLeftReferencePoint )
text1.x = 50

-- Center alignment, multiple lines
local textString2 = "Line 1 center aligned\nLine2 center"
local textOptions2 = { text = textString2, x = 150, y = 100, width = 200, height = 0, align = "center", font = native.systemFont, fontSize = 14 }
local text2 = display.newText( textOptions2 )
text2:setReferencePoint( display.TopLeftReferencePoint )
text2.x = 50

-- No alignment specified
local textOptions3 = { text = "Line defaults to left aligned", x = 200, y = 150, width = 200, font = native.systemFont, fontSize = 14 }
local text3 = display.newText( textOptions3 )
text3:setReferencePoint( display.TopLeftReferencePoint )
text3.x = 50

Text justification also works with custom fonts but you must install the fonts on the Mac or Windows simulator in order to make it work.

5. Does display.newEmbossedText work with justification?

Currently, text justification for display.newEmbossedText objects is not implemented. We hope to have it in a Daily Build soon.

That’s it for today’s questions. I hope you enjoyed them and even learned a few things.

  1. Would be nice to have 2 features:

    Related to text:
    – simple version of display.newText for showing score etc so with numbers only and with prerendered images etc… without need to render it every time. This would be nice to have timers or score counters with very fast update time.

    Not related to text:
    – make a screenshot feature inside Corona Simulator, i miss this a lot on Windows (on Mac this is build in OS).

    Tom

    • Dennis Koble says:

      You can of course always use Alt-Prt Scr to capture the Corona window.
      I use it all the time and then paste it into mspaint.exe and save out as a png file.
      Not quite what you asked for but it is quite useful.
      -Dennis

  2. I’m trying to display long text but I’m seeing white blocks. I know I have to create for each line text object. Can you open source the way corona wraps text? I mean a function that we pass the text, font, size and then break it into a table.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>