Cannot center text?

How can I center-align text? The following doesn’t work for me, it’s still left-aligned.

sprite:setReferencePoint(display.CenterReferencePoint) sprite.x = 100 sprite.y = 100

Note the text has been given a width and height during creation (without it, I get odd font problems in the Windows simulator running iPad). Thanks! [import]uid: 10284 topic_id: 23470 reply_id: 323470[/import]

Is it possible it is center aligned but the extra width is making it not appear so? [import]uid: 52491 topic_id: 23470 reply_id: 94232[/import]

Peach, thanks for replying, the *object* is center-aligned all-right – in its full width, yes – but the problem is that the *text* is not center-aligned. Within the centered object, the text itself is left-aligned, so it’s off. My question: How can I center-align the *text* itself?

E.g. like this (note how the text is of variable length, i.e. it becomes shorter during the countdown):

-----------------------------
| |
| |
| |
| [125 Seconds Left] |
| | 
| |
| |
-----------------------------

Thanks! [import]uid: 10284 topic_id: 23470 reply_id: 94233[/import]

Aren’t display object standard center referenced?
Something like this shows a count down with the text centered on the box.

[code]

local myGroup = display.newGroup()
local myRect = display.newRect( 50, 190, 200, 100)
myGroup:insert(myRect)
local myText = display.newText( “”, 0, 0, native.systemFont, 30)
myText:setTextColor(100)
myGroup:insert(myText)
myText.x = myRect.x
myText.y = myRect.y
local startValue = 400

local function onFrame( event)
myText.text = startValue
startValue = math.floor(startValue - 0.1)
if startValue < 0 then Runtime:removeEventListener(“enterFrame”, onFrame) end
end

Runtime:addEventListener(“enterFrame”, onFrame)
[/code] [import]uid: 123200 topic_id: 23470 reply_id: 94247[/import]

Dutchottie, they are, but only if you leave out the optional width and height parameter – but if you do that, things start to look very broken when I try here in the Windows simulator (latest build, 767). The whole font becomes very wrongly stretched in some way (IIRC this didn’t happen in much earlier versions of Corona). When I define a width and height, the font looks good again, but I lose the center-alignment.

Edit: Note the oddly stretched font seems to be an issue only on the Windows simulator – potentially on Android devices – not in the Mac simulator. [import]uid: 10284 topic_id: 23470 reply_id: 94256[/import]

Hi Philipp,

Do you really need the width and height parameters?
As I understand they’re needed for multi-line texts. If you’re using just 1 line, not specifying the w and h make the centering a piece of cake (as you know).
An even then, perhaps it’s easier to use several 1-line objects that you can control, than one multi-line box that you can’t :slight_smile: [import]uid: 123200 topic_id: 23470 reply_id: 94260[/import]

Dutchottie, thanks, indeed I am now switching back to not using width and height. I switched to using width and height for the text object because it rendered horribly in the Win-Simulator but worked on the device [Edit: that font stretching was an issue on my end]. [import]uid: 10284 topic_id: 23470 reply_id: 94264[/import]