Skip to content
ExamplesOpen editor ↗

bopharaMokwalo

→ textWidth()

Measures how wide a piece of text is, in pixels, without drawing it.

bopharaMokwalo(words)

It returns a number. It asks: if I drew these words right now, how wide would they be? The answer depends on bogoloJwaMokwalo, mofutatlhaka and mokgwaWaMokwalo — change any of them and the number changes.

It is what you use to place text yourself: to centre it, to draw a box behind it, or to break lines by hand.

Two things that may not be what you expect:

  • When the text has several lines it returns the width of the longest line, not the total.
  • Leading and trailing spaces are ignored. bopharaMokwalo(' Dumela ') equals bopharaMokwalo('Dumela').
NameTypeDescription
mafokomokwaloThe text to measure.
A box that fits the text
tiro setaLoeto() {
  thalaKanefase(400, 200);
  bokamorago(240);
  bogoloJwaMokwalo(28);

  metlha mafoko = 'Dumela, Botswana';
  metlha b = bopharaMokwalo(mafoko);

  tlatsaMmala(250, 220, 150);
  tlogelaMothalo();
  thalaKhutlonne(40, 76, b + 24, 44);

  tlatsaMmala(30, 40, 60);
  thalaMokwalo(mafoko, 52, 108);

  kwala(b);

  tlhalosa('Mafoko a a mo lebokosong le le mmala wa lonaka le le lekanyeditsweng go a lekana.');
}

thalaMokwalo, bogoloJwaMokwalo, mofutatlhaka, bophara