How to center ALIGN text?

I’m writing an application involving lots of text, and in this case it looks bad aligned to the left. I was wondering how I could make it so that the text aligns to the center of the screen? I already have the object placed in the center, but the text is aligned to the left. Thank you! [import]uid: 66386 topic_id: 23175 reply_id: 323175[/import]

Look at the [lua]object:setReferencePoint()[/lua] API’s on the Corona SDK website.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 23175 reply_id: 92683[/import]

myText:setReferencePoint(display.CenterReferencePoint)
myText.x = 100

Like Jordan said above but figured I’d throw an example up too :slight_smile: [import]uid: 52491 topic_id: 23175 reply_id: 92705[/import]

I’m sorry, but I don’t see how you answers could have solved this problem.

I’m in the same boat where I have text displayed with a normal text object, with no width or height entered the text is single-lined and is aligned in the center, however, when you add width and height to make the text multi-lined it aligns itself to the left.

Changing the reference point doesn’t change this.

Example

[code]
–[[
This is the standard single-lined text object

This is what it looks like when it supports multiple lines
Which is aligned to the left. This doesn’t look good
in my game.

This is what we’re asking for, a multi-lined
text object that aligns the text
to the center rather than to the left
as demonstrated above.
–]] [import]uid: 129450 topic_id: 23175 reply_id: 115129[/import]

+1 on this. [import]uid: 93133 topic_id: 23175 reply_id: 115143[/import]

+1!

This one is other thing that would make our SDK even BETTER with doing so little. :slight_smile:

Have a look into it Corona Labs Team, please.
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 23175 reply_id: 115236[/import]

Hey guys, I think there was some misunderstanding with the original post.

Center align text:
[lua]local myText = native.newTextBox(10, 10, 300, 300)
myText.text = “This is a test text box”
myText.align = “center”[/lua]

You must build for device or simulator, this shows left on Corona simulator but it will center on Xcode simulator.

http://developer.coronalabs.com/node/2601

Peach :slight_smile: [import]uid: 52491 topic_id: 23175 reply_id: 115346[/import]

@Peach,

Thank you! :slight_smile:
Rodrigo. [import]uid: 89165 topic_id: 23175 reply_id: 115362[/import]

Thanks Peach!

However, it would be great if we could do this with with the non-native display.newText, and of course that it would show up “right” in the simulator as well :slight_smile: [import]uid: 129450 topic_id: 23175 reply_id: 115504[/import]

Check out Crawlspace Lib, theres a paragraph function that allows alignment of text. Either use that function or take that code and re-write it with the recent newText(). [import]uid: 13560 topic_id: 23175 reply_id: 115507[/import]

If you want similar behaviour for normal text obj you can do this like this (but you have to test and manualy split text using \n) .
With some additional work you could do this automatically wrapping text to the given width

[code]

function string:split(sep)
local sep, fields = sep or “:”, {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function© fields[#fields+1] = c end)
return fields
end

local function alignText( myText, offsetLine)
local displayMessage = myText:split("\n")
local msgGroup = display.newGroup()
msgGroup.isVisible = false
local offset = offsetLine
msgGroup:setReferencePoint(display.TopCenterReferencePoint)
if offset ==nil then offset = 2 end-- this is for defining text spacing, it can be function argument or constant
local lastPos = 0
for i=1, #displayMessage do
msgGroup:setReferencePoint(display.TopCenterReferencePoint)
local txt = display.newText(displayMessage[i], 0, 0, “Gotham Bold”, 16)
msgGroup:insert(txt)
msgGroup:setReferencePoint(display.TopCenterReferencePoint)
txt:setReferencePoint(display.TopCenterReferencePoint)
txt.x, txt.y = msgGroup.x,lastPos + offset
txt:setTextColor(255,255,255) – should be function argument, with other text parameters like font, size etc.
lastPos = lastPos + txt.height

end

return msgGroup
end
local testString = “This is what it looks like when it supports multiple lines\nWhich is aligned to the left. This doesn’t look good\nin my game.”
local msg = alignText(testString)
msg.x, msg.y, msg.isVisible = 200, 200, true
[/code] [import]uid: 145499 topic_id: 23175 reply_id: 120837[/import]

If you want similar behaviour for normal text obj you can do this like this (but you have to test and manualy split text using \n) .
With some additional work you could do this automatically wrapping text to the given width

[code]

function string:split(sep)
local sep, fields = sep or “:”, {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function© fields[#fields+1] = c end)
return fields
end

local function alignText( myText, offsetLine)
local displayMessage = myText:split("\n")
local msgGroup = display.newGroup()
msgGroup.isVisible = false
local offset = offsetLine
msgGroup:setReferencePoint(display.TopCenterReferencePoint)
if offset ==nil then offset = 2 end-- this is for defining text spacing, it can be function argument or constant
local lastPos = 0
for i=1, #displayMessage do
msgGroup:setReferencePoint(display.TopCenterReferencePoint)
local txt = display.newText(displayMessage[i], 0, 0, “Gotham Bold”, 16)
msgGroup:insert(txt)
msgGroup:setReferencePoint(display.TopCenterReferencePoint)
txt:setReferencePoint(display.TopCenterReferencePoint)
txt.x, txt.y = msgGroup.x,lastPos + offset
txt:setTextColor(255,255,255) – should be function argument, with other text parameters like font, size etc.
lastPos = lastPos + txt.height

end

return msgGroup
end
local testString = “This is what it looks like when it supports multiple lines\nWhich is aligned to the left. This doesn’t look good\nin my game.”
local msg = alignText(testString)
msg.x, msg.y, msg.isVisible = 200, 200, true
[/code] [import]uid: 145499 topic_id: 23175 reply_id: 120837[/import]

if you want to center align display.newText, you set its x position after you create it and set the reference point. you also have to provide a reference object to center it against (could be its own group, in this case it’s an image)

[code]
local group = display.newGroup()

local polaroid = display.newImage(‘img/polaroid.png’, 0, 0)
group:insert(polaroid)

local text = display.newText(self.name, 0, 350, “ChelseaMarket-Regular”, 30)
text:setReferencePoint( display.CenterReferencePoint )
text.x = polaroid.width/2 – set center to middle of image
text:setTextColor(255, 255, 255)
group:insert(text)
[/code] [import]uid: 121083 topic_id: 23175 reply_id: 122163[/import]

if you want to center align display.newText, you set its x position after you create it and set the reference point. you also have to provide a reference object to center it against (could be its own group, in this case it’s an image)

[code]
local group = display.newGroup()

local polaroid = display.newImage(‘img/polaroid.png’, 0, 0)
group:insert(polaroid)

local text = display.newText(self.name, 0, 350, “ChelseaMarket-Regular”, 30)
text:setReferencePoint( display.CenterReferencePoint )
text.x = polaroid.width/2 – set center to middle of image
text:setTextColor(255, 255, 255)
group:insert(text)
[/code] [import]uid: 121083 topic_id: 23175 reply_id: 122163[/import]

Are you sure this works, as for me it doesn’t appear to.
I have a line that goes - Time: number and it annoyingly shifts position as the number changes.
There must be a way to correct this, but just setting the reference point didn’t do it for me. [import]uid: 125838 topic_id: 23175 reply_id: 127422[/import]

It works, but each time when you modify text you have to set reference point AND set all the the time this same X position.
[import]uid: 145499 topic_id: 23175 reply_id: 127429[/import]

Are you sure this works, as for me it doesn’t appear to.
I have a line that goes - Time: number and it annoyingly shifts position as the number changes.
There must be a way to correct this, but just setting the reference point didn’t do it for me. [import]uid: 125838 topic_id: 23175 reply_id: 127422[/import]

It works, but each time when you modify text you have to set reference point AND set all the the time this same X position.
[import]uid: 145499 topic_id: 23175 reply_id: 127429[/import]