You need to account for the baseline of the string using g.FontAscent
. Try this in a Canvas’ Paint event:
g.DrawingColor = &cff0000
g.DrawRectangle( 0, 0, g.Width, g.Height )
var textToDraw as String = "This is a test string."
var textWidth as Double = g.TextWidth( textToDraw )
var textX as Double = (g.Width - textWidth) / 2
var textY as Double = ((g.height - g.TextHeight) / 2) + g.FontAscent
g.DrawText( textToDraw, textX, textY, textWidth )