# Draw text with 2 colors in viewport

I'd like to know if it's possible to display text, with blf module, in the viewport with 2 colors ? For exemple, i want to draw "Ngon: 5".

"Ngon:" should be red and "5" should be blue.

Did i have to draw my text separatly or is it possible to define the color in the string ?

yeah, you have to draw by setting a bgl.color** before each new differently coloured text. It's not like a terminal print command, though you could write really simple functions that take

(x, y, [(str, col),...])


Because you can query the x,y dimensions of a string, you can easily generate the offset coordinates.

Something like this:

def draw_string(x, y, packed_strings):
font_id = 0
blf.size(font_id, 18, 72)
x_offset = 0
for pstr, pcol in packed_strings:
bgl.glColor4f(*pcol)
text_width, text_height = blf.dimensions(font_id, pstr)
blf.position(font_id, (x + x_offset), y, 0)
blf.draw(font_id, pstr)
x_offset += text_width

x = 60
y = 100
RED = (1, 0, 0, 1)
GREEN = (0, 1, 0, 1)
BLUE = (0, 0, 1, 1)
ps = [("Blue ", RED),("Yellow ", BLUE),("White ", GREEN)]
draw_string(x, y, ps)


• Just on more question. Is it possible to draw other line under the previous one with an array fonction, still conserved the colors ? – pistiwique May 31 '15 at 19:43
• please ask a new separate question. – zeffii May 31 '15 at 19:44