Canvas:measure(text) method


Measure text size on the Canvas

Parameters

text

A string representing the text to be measured

Return value

This function returns the text size as a table with two numeric fields: width and height

Example

local ui = require "ui" require "canvas" -- create a simple Window local win = ui.Window("Canvas.measure() sample - Move the mouse over the window", "fixed", 500, 400) -- create a Canvas local canvas = ui.Canvas(win) canvas.align = "all" canvas.font = "Impact" canvas.fontsize = 40 -- create a RadialGradient local radial = canvas:RadialGradient { [0] = 0x00FFFFFF, [1] = 0x00008BFF } radial.radius = { 400, 400 } local text = "Hello LuaRT World !!!" -- center the text local size = canvas:measure(text) local xpos = (canvas.width - size.width)/2 local ypos = (canvas.height - size.height)/2 function canvas:onPaint() canvas:clear() canvas:print(text, xpos, ypos, radial) end function canvas:onHover(x, y) radial.center = { x, y } end win:show() -- update user interface repeat ui.update() until not win.visible