Text alignment automatically changes.

Hi,

How can I make my text objects aligned to be left aligned? I tested everything I could find from setReferencePoint to object.align, it’s fine until I update it’s text then it changes it’s alignment automatically and does some kind of center justified alignment.

How can I prevent this?

My text objects are not long so should not have anything to do with wrapping.

Thanks. [import]uid: 206803 topic_id: 35354 reply_id: 335354[/import]

All display objects are TopLeft aligned during creation, but Center aligned AFTER creation.

To ensure that alignment is right, set the alignment and position after an update.

textObject.originalX = textObject.x textObject.originalY = textObject.y textObject.text = "something else" textObject:setReferencePoint(display.TopLeftReferencePoint) textObject.x = textObject.originalX textObject.y = textObject.originalY

That code could easily be stuffed into a single function to update it. [import]uid: 41884 topic_id: 35354 reply_id: 140530[/import]

Thanks. Do I have to do this for every text object after each text update? [import]uid: 206803 topic_id: 35354 reply_id: 140540[/import]

You don’t have to, but the effect of your text change can be a bit unpredictable, particularly if you’re also moving the text object onscreen. If you always want text in the same position and alignment, you’d probably want to just write a function like this:

[code] – function
local function setText(self, string)
self.text = string or self.text
self:setReferencePoint(display.TopLeftReferencePoint)
self.x = self.originalX or self.x
self.y = self.originalY or self.y
end

– make the text object
local object = display.newText(“Hi there!”)
object:setReferencePoint(display.TopLeftReferencePoint)
object.x = 32
object.y = 48
object.originalX = object.x
object.originalY = object.y
object.setText = setText

– how to use:
object:setText(“Wait a minute!”) – whatever string you use with this is always TL aligned and using originalX/originalY as alignment[/code] [import]uid: 41884 topic_id: 35354 reply_id: 140544[/import]

Thanks man, that did it.

Why does Corona do this strange behavior of aligning text objects? Is it to make it work in some other scenario? [import]uid: 206803 topic_id: 35354 reply_id: 140637[/import]

All display objects are TopLeft aligned during creation, but Center aligned AFTER creation.

To ensure that alignment is right, set the alignment and position after an update.

textObject.originalX = textObject.x textObject.originalY = textObject.y textObject.text = "something else" textObject:setReferencePoint(display.TopLeftReferencePoint) textObject.x = textObject.originalX textObject.y = textObject.originalY

That code could easily be stuffed into a single function to update it. [import]uid: 41884 topic_id: 35354 reply_id: 140530[/import]

Thanks. Do I have to do this for every text object after each text update? [import]uid: 206803 topic_id: 35354 reply_id: 140540[/import]

You don’t have to, but the effect of your text change can be a bit unpredictable, particularly if you’re also moving the text object onscreen. If you always want text in the same position and alignment, you’d probably want to just write a function like this:

[code] – function
local function setText(self, string)
self.text = string or self.text
self:setReferencePoint(display.TopLeftReferencePoint)
self.x = self.originalX or self.x
self.y = self.originalY or self.y
end

– make the text object
local object = display.newText(“Hi there!”)
object:setReferencePoint(display.TopLeftReferencePoint)
object.x = 32
object.y = 48
object.originalX = object.x
object.originalY = object.y
object.setText = setText

– how to use:
object:setText(“Wait a minute!”) – whatever string you use with this is always TL aligned and using originalX/originalY as alignment[/code] [import]uid: 41884 topic_id: 35354 reply_id: 140544[/import]

Thanks man, that did it.

Why does Corona do this strange behavior of aligning text objects? Is it to make it work in some other scenario? [import]uid: 206803 topic_id: 35354 reply_id: 140637[/import]