GetTextWidth

Returns the width of a line of text when drawn with this font. Use this to center text or calculate background dimensions.

Syntax

Parameters

Returns

Returns the width of the specified text.

Example

window=Window:Create()
context=Context:Create(window)
font = Font:Load("Fonts/arial.ttf",36)
context:SetFont(font)


while true do
if window:Closed() or window:KeyHit(Key.Escape) then return false end

context:SetColor(0,0,0)
context:Clear()
context:SetColor(1,1,1)

--Display some centered text
local text = "Leadwerks"
local screenwidth = window:GetWidth()
local screenheight = window:GetHeight()
local x = (screenwidth - font:GetTextWidth(text))/2
local y = (screenheight - font:GetHeight())/2

context:SetBlendMode(Blend.Alpha)
context:DrawText(text,x,y)
context:SetBlendMode(Blend.Solid)

context:Sync()
end