Well today just might be you’re lucky day, that is, if you’re one of those people in need of it, because as of Daily Build 638, display.newText() now has the ability to wrap text, and it now properly supports the newline (n) character! (Be sure to see the Important Notes section at the end)
Here’s the new syntax:
display.newText( string, x, y, [w, h,] font, size )
To enable the text-wrapping capabilities, you must specify the new w and h arguments, which correspond to the width and height (in pixels) of your invisible text area.
Here’s a few examples to demonstrate the new functionality:
Example 1: Support for the newline (n) character:
local textMessage = display.newText( "Hello Corona User!nHope you're having a great day.", 25, 25, "Helvetica", 18 )
textMessage:setTextColor( 0 )
The above code should show this in the simulator screen:
Hello Corona User!
Hope you're having a great day.
Example 2: Text wrapping
local multiText = display.newText( "This text should be wrapped according to the specified width.", 25, 25, 175, 400, "Helvetica", 18 )
multiText:setTextColor( 0 )
The above example will begin wrapping text once it reaches the 175 pixel width we set when calling display.newText().
- Currently, these changes are only available on the Simulator and on iOS devices (not on Android yet).
- Previously, the newline (n) character was supported in the simulator but not on any devices.
- If text goes beyond the specified h (height) parameter, text will be cropped, so be mindful of that.
- Fonts vary slightly, for example, line spacing may be different in the simulator vs. on device, and native.systemFont on the Mac is different than in iOS—so be sure to do extensive on-device testing as you go.
And speaking of Daily Builds, remember the new homescreen you read about the other day? The ‘New Project’ button, which was previously non-functional, is now working (since build 638)—so go try it out!