Skip to content

textWidth()

Calculates the maximum width of a string of text drawn when text() is called.

Examples

textWidth example 1

function setup()
  size(100, 100)

  background(200)

  -- Style the text.
  textSize(28)
  strokeWeight(0.5)

  -- Calculate the text width.
  local s = 'yoyo'
  local w = textWidth(s)

  -- Display the text.
  text(s, 22, 55)

  -- Underline the text.
  line(22, 55, 22 + w, 55)

  describe('The word "yoyo" underlined.')
end

textWidth example 2

function setup() 
  size(100, 100)

  background(200)

  -- Style the text.
  textSize(28)
  strokeWeight(0.5)

  -- Calculate the text width.
  -- "\n" starts a new line.
  local s = 'yo\nyo'
  local w = textWidth(s)

  -- Display the text.
  text(s, 22, 55)

  -- Underline the text.
  line(22, 55, 22 + w, 55)

  describe('The word "yo" written twice, one copy beneath the other. The words are divided by a horizontal line.')
end

Syntax

textWidth(str)

Parameters

Parameter
str String: string of text to measure