The draw.text() function

Display text with the draw.text() function

To avoid confusion, I recommend that you do NOT use the functions described on this page unless strictly necessary because the functions text_pixels() and text_grid() are easier to use.

The draw.text() function is very similar to the text_pixels() except that it adds the possibility of setting a fill (background) color for the text. It is formatted differently to text_pixels() which is why I recommend you stick to using text_pixels() and text_grid() to avoid confusion. The draw_text() function requires that the coordinates be given as a tuple and the code to specify alternate fonts is different too.

The official documentation for the functions described on this page can be found HERE. 

This script will display 'Hello, World' as small white text in a black rectangle with 'THIS TEXT IS BLACK' displayed in black in the lower half of the screen. The coordinates in the parameter of the text() function refer to the top left corner of the text.

#!/usr/bin/env python3

from ev3dev2.display import Display

from time import sleep

lcd = Display()

lcd.draw.rectangle((0,0,177,40), fill='black')

lcd.draw.text((48,13),'Hello, world.', fill='white')

lcd.draw.text((36,80),'THIS TEXT IS BLACK')

lcd.update()

sleep(10)

Here is the result of running the script:

In this image it is hard to see where the edges are since much of the image has white edges against a white page, which is annoying. We'll see a solution for that in a moment.

Center Text

The textsize() function is not used to set the size of text, it is used to get the size (length and height) of a text string in pixels. For example, it might return the tuple (111, 15). The first element of the tuple (element zero) can be used to center text horizontally as shown below. We could use the second element to calculate where to place the text vertically but it's easier to do a quick mental calculation: To center text vertically just subtract half the text height from 64. The center of the screen can be assumed to be at (89, 64). This script also prints the value of tuple returned by textsize().

#!/usr/bin/env python3

# aligns text to the centre horizontally and vertically. 

from ev3dev2.display import Display

from time import sleep

screen = Display()

mystring = 'EV3 Python rules!'

# give the screen a light grey background

lcd.draw.rectangle((0,0,177,127), fill='yellow')

size = lcd.draw.textsize(mystring) # returns a tuple

lcd.draw.text((20, 20), 'textsize = '+str(size))

# lcd height = 128 pixels, so vertical center is

# at 64 pixels, so place text (height 11 pixels) at

# 5 pixels above the center, at 59

lcd.draw.text((89-size[0]/2, 59), mystring)

lcd.update()

sleep(10)

Here is the result:

Note how this image has a light grey background which makes the edges easier to see. The code requested that a rectangle filling the screen should be drawn with a yellow fill but the screen is greyscale so a light grey was used. I could also have requested fill='grey' but that would have made the background much darker.

Wrap text

The print() function wraps text automatically but the draw.text() function does not. The following script wraps the text onto a new line after every 30 characters. This isn't proper 'word wrap' since it does not try to detect the spaces between words. Notice how mystring is made up of a string that is 'multiplied by 7'.

#!/usr/bin/env python3

# wraps text to a new line after every 30 characters

from ev3dev2.display import Display

from time import sleep

lcd = Display()

mystring = 'This is a very long string! '*7

newstring=''

for i in range(len(mystring)):

    # len(mystring) gives the number of characters

    newstring = newstring+mystring[i]

    if i%30 == 29:  # modulo, or remainder after division

        newstring = newstring+'\n'

        # \n is the new line character

lcd.draw.text((0,0), newstring)

lcd.update()

sleep(10)

Here is the result:

Use larger fonts

The draw.text() function uses a tiny font by default but larger fonts can be used. The range of available fonts is the same as for text_pixels() and text_grid() but the coding is different. To load the desired font you must use font=fonts.load('luBS14'). The capital letters in the names of the fonts have the following meanings: B= Bold, I=Italic, O=Oblique (similar to italic), S=?, R=? As an example I will use the luBS14 font (bold, size 14) and the luBS24 font (bold, size 24).

#!/usr/bin/env python3

import ev3dev2.fonts as fonts

from ev3dev2.display import Display

from time import sleep

lcd = Display()

lcd.draw.text((10,20), 'Hello World!', font=fonts.load('luBS14'))

lcd.draw.text((10,60), 'Hello World!', font=fonts.load('luBS24'))

lcd.update()

sleep(8)

Here is the result:

To make the background light grey so that the screenshot edges are visible I used the same code used previously but I have removed it from the sample project above to simplify the code and since you will not usually need to use this trick yourself. I do the same elsewhere in this site.