I just finished to polish my "Score" output and was not very pleased about the text positioning weirdness so far :(
Well... Here some code I just found to work perfectly for updating text in a left aligned manner without using all the overhead ui.newLable() introduces.
I am sure the same technique works for right alignment and centering with little modification!
Here an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Foo:updateScore() local txt="Score: "..self.current_score if self.scoreview then local x=self.scoreview.x self.scoreview.text=txt self.scoreview:setReferencePoint(display.TopLeftReferencePoint) self.scoreview.x=x else self.scoreview = display.newText(txt, 10, 30, nil, 18 ) self.scoreview:setTextColor( 255,255,255 ) self.scoreview:setReferencePoint(display.TopLeftReferencePoint) end end |
You will notice those "strange" calls to setReferencePoint() ... this is the "trick". Without those you will not get consistent placement!
P.S.: Actually I think it is a bug that the ReferencePoint is changing at the assignment to the obj.text attribute!
Great tip and solution. I was playing with setReferencePoint trying to get my stuff to work but didn't realize that it was a bug until I did some searching.
BTW, you're right about it being a bug: #189
http://developer.anscamobile.com/forum/2010/04/23/textobjectsetreferencepoint-requires-setting-xy-coordinates-again
I started keeping my own list of bugs so I don't keep hitting my head against the wall reinventing the same bug. :)
Tom