Dear Corona-community,
I am still working on my emoji-module, currently facing a weird behaviour with y-alignment of fonts.
Here is what I’d like to do:
I’d like to align display.newText() - elements and display.newImageRect() - elements.
What I’ve done so far:
Spent the last 14 hours on researching the graphics.getfontMetrics API plus textObject.baselineOffset - method.
This is a very simplified version, demonstration the issue I’d like to address:
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local top = display.screenOriginY local center = display.screenOriginX + display.contentWidth/2 local fontSize = 14 -- create white squares with fontSize as height local line1 = display.newRect(center, top + 50, display.contentWidth, fontSize) line1:setFillColor(1) local line2 = display.newRect(center, top + 100, display.contentWidth, fontSize) line2:setFillColor(1) local line3 = display.newRect(center, top + 150, display.contentWidth, fontSize) line3:setFillColor(1) local line4 = display.newRect(center, top + 200, display.contentWidth, fontSize) line4:setFillColor(1) local line5 = display.newRect(center, top + 250, display.contentWidth, fontSize) line5:setFillColor(1) -- get font-metrics local metrics1 = graphics.getFontMetrics( native.systemFont, fontSize ) local metrics2 = graphics.getFontMetrics( "AvenirLTStd-Roman.otf", fontSize ) local metrics3 = graphics.getFontMetrics( "BreeSerif-Regular.ttf", fontSize ) local metrics4 = graphics.getFontMetrics( "FredokaOne-Regular.ttf", fontSize ) local metrics5 = graphics.getFontMetrics( "PatrickHand-Regular.ttf", fontSize ) -- create text-objects local text1 = display.newText("Asc: " .. string.sub(metrics1.ascent, 1, 5) .. " Desc: " .. string.sub(metrics1.descent, 1, 5) .. " Hght: " .. string.sub(metrics1.height, 1, 5) .. " Lding: " .. string.sub(metrics1.leading, 1, 5), line1.x, line1.y, native.systemFont, fontSize) text1:setFillColor(0) local text2 = display.newText("Asc: " .. string.sub(metrics2.ascent, 1, 5) .. " Desc: " .. string.sub(metrics2.descent, 1, 5) .. " Hght: " .. string.sub(metrics2.height, 1, 5) .. " Lding: " .. string.sub(metrics2.leading, 1, 5), line2.x, line2.y, "AvenirLTStd-Roman.otf", fontSize) text2:setFillColor(0) local text3 = display.newText("Asc: " .. string.sub(metrics3.ascent, 1, 5) .. " Desc: " .. string.sub(metrics3.descent, 1, 5) .. " Hght: " .. string.sub(metrics3.height, 1, 5) .. " Lding: " .. string.sub(metrics3.leading, 1, 5), line3.x, line3.y, "BreeSerif-Regular.ttf", fontSize) text3:setFillColor(0) local text4 = display.newText("Asc: " .. string.sub(metrics4.ascent, 1, 5) .. " Desc: " .. string.sub(metrics4.descent, 1, 5) .. " Hght: " .. string.sub(metrics4.height, 1, 5) .. " Lding: " .. string.sub(metrics4.leading, 1, 5), line4.x, line4.y, "FredokaOne-Regular.ttf", fontSize) text4:setFillColor(0) local text5 = display.newText("Asc: " .. string.sub(metrics5.ascent, 1, 5) .. " Desc: " .. string.sub(metrics5.descent, 1, 5) .. " Hght: " .. string.sub(metrics5.height, 1, 5) .. " Lding: " .. string.sub(metrics5.leading, 1, 5), line5.x, line5.y, "PatrickHand-Regular.ttf", fontSize) text5:setFillColor(0)
You can download a zipped version with fonts included here:
Now, this is how it looks on Android devices:
That looks perfectly fine to me. Notice that the second line is the only line where metrics.leading has an actual value other than 0. But still, this line is aligned fine. Now check out how it looks on an iPhone:
Notice how this same second line is not aligned any more as it is supposed to be. If you add the metrics.ascending value and the metrics.descending value, you get the metrics.height value, but the metrics.leading value is not considered, in contrast to Android, where metrics.leading is part of metrics.height. Hope it gets clear what I’m trying to say.
I don’t see this is expected behaviour, but maybe I’m overseeing something 
Could anyone have a look at this?
Thanks