Sets the current alignment for drawing text.


example picture for text_align()
1def setup():
2    py5.background(0)
3    py5.text_size(16)
4    py5.text_align(py5.RIGHT)
5    py5.text("ABCD", 50, 30)
6    py5.text_align(py5.CENTER)
7    py5.text("EFGH", 50, 50)
8    py5.text_align(py5.LEFT)
9    py5.text("IJKL", 50, 70)
example picture for text_align()
 1def setup():
 2    py5.background(0)
 3    py5.stroke(153)
 4    py5.text_size(11)
 5    py5.text_align(py5.CENTER, py5.BOTTOM)
 6    py5.line(0, 30, py5.width, 30)
 7    py5.text("CENTER,BOTTOM", 50, 30)
 8    py5.text_align(py5.CENTER, py5.CENTER)
 9    py5.line(0, 50, py5.width, 50)
10    py5.text("CENTER,CENTER", 50, 50)
11    py5.text_align(py5.CENTER, py5.TOP)
12    py5.line(0, 70, py5.width, 70)
13    py5.text("CENTER,TOP", 50, 70)


Sets the current alignment for drawing text. The parameters LEFT, CENTER, and RIGHT set the display characteristics of the letters in relation to the values for the x and y parameters of the text() function.

An optional second parameter can be used to vertically align the text. BASELINE is the default, and the vertical alignment will be reset to BASELINE if the second parameter is not used. The TOP and CENTER parameters are straightforward. The BOTTOM parameter offsets the line based on the current text_descent(). For multiple lines, the final line will be aligned to the bottom, with the previous lines appearing above it.

When using text() with width and height parameters, BASELINE is ignored, and treated as TOP. (Otherwise, text would by default draw outside the box, since BASELINE is the default setting. BASELINE is not a useful drawing mode for text drawn in a rectangle.)

The vertical alignment is based on the value of text_ascent(), which many fonts do not specify correctly. It may be necessary to use a hack and offset by a few pixels by hand so that the offset looks correct. To do this as less of a hack, use some percentage of text_ascent() or text_descent() so that the hack works even if you change the size of the font.

Underlying Processing method: textAlign


text_align(align_x: int, /) -> None
text_align(align_x: int, align_y: int, /) -> None


  • align_x: int - horizontal alignment, either LEFT, CENTER, or RIGHT

  • align_y: int - vertical alignment, either TOP, BOTTOM, CENTER, or BASELINE

Updated on November 12, 2021 11:30:58am UTC