When we need to know the size of text, there are three functions:
pd.gfx.getTextSizeForMaxWidth(text, maxWidth)
gfx.font:getTextWidth(text)
andgfx.font:getHeight()
gfx.font:drawText(text, x, y)
returnswidth, height
The last one drawText
returns a different size from the other two methods. That is, the width, height
returned from getTextSizeForMaxWidth
returns the same values as getTextWidth
and getHeight
; while drawText
always returns a shorter/smaller height value.
Same result using default font or a custom font: functions return mismatched height values.
Example code:
-- default font
local font = gfx.getFont()
-- using (g)raphics
local gWidth, gHeight = gfx.getTextSizeForMaxWidth("EDIT", pd.display.getWidth())
-- gWidth = 32, gHeight = 20
-- using (f)ont
local fWidth, fHeight = font:getTextWidth("EDIT"), font:getHeight()
-- fWitdh = 32, fHeight = 20
-- (d)rawn
local dWidth, dHeight = font:drawText("EDIT", 0,0)
-- dWidth = 32, fHeight = 14
Changing the string to `"graphics", these sizes are returned:
- 63, 20 - graphics
- 63, 20 - font
- 63, 17 - drawn