Thanks for the replies. Sorry about the sloppy code, my brain was running out of steam when I posted and I wasn’t thinking at my best (which isn’t great at the best of times…).
I’m not sure I understand the problem with positioning things by width. If I place one object on top of another, and then move it left or right by half of each of their widths, they should be flush. I’ve done this lots of times with other display objects without problem.
I’ve tried it with a fixed-width font and it’s the same problem.
Some investigation seems to suggest the issue only occurs when the first part of “conjoined” text objects is made from a single character:
local left = display.screenOriginX
local top = display.screenOriginY
local fontSize = 16
local function make(firstString, secondString, y)
local whole = display.newText{text = firstString..secondString, y = y, fontSize = fontSize}; whole:setFillColor(1,0,0)
local firstPart = display.newText{text = firstString, fontSize = fontSize}
local secondPart = display.newText{text = secondString, fontSize = fontSize}
whole.x = left + whole.width/2
firstPart.x = left + firstPart.width/2
secondPart.x = firstPart.x + firstPart.width/2 + secondPart.width/2
firstPart.y = whole.y + whole.height*0.7
secondPart.y = firstPart.y
local combinedWidth = firstPart.width + secondPart.width
print(whole.width, combinedWidth)
end
make("a", "b", top + fontSize/2)
make("a", "bb", top + 50)
make("aa", "b", top + 100)
make("aa", "bb", top + 150)
A quick test suggests I can resolve this by checking that the first part of a conjoined pair is a single character, and if it is, place the second part as normal, but move it back by an amount equal to half the difference between the width of the whole and the sum of the widths of the two parts. Sorry if that’s confusing. Hopefully you can figure out what I mean.
I’ll let you know how it goes.