How to change style while rendering freetype.Font
Question:
I want to be able to use bold or italic styles while rendering with pygame.freetype.Font
, but it turns out freetype.Font.render
doesen’t take any bold
or italic
keyword arguments.
Instead it takes a style
argument. I’m not sure how to use this though, please help me.
import pygame
from pygame import freetype
freetype.init()
font = freetype.Font(“fontfile.ttf”,50)
win = pygame.display.set_mode((500,500))
while True:
for e in pygame.event.get():
if e.type == pygame.quit():
quit()
text,rect = font.render(“TEXT”,(255,255,255),style=?????)
win.fill((0,0,0))
win.blit(text,rect)
pygame.display.update()
Answers:
- to create bold text you can pass the
pygame.freetype.STYLE_STRONG
constant:
text, rect = font.render("TEXT", (255, 255, 255), style=pygame.freetype.STYLE_STRONG)
- To create italic text you can pass the
pygame.freetype.STYLE_ITALIC
constant:
text, rect = font.render("TEXT", (255, 255, 255), style=pygame.freetype.STYLE_ITALIC)
- If you want to create text that is both bold and italic, you can pass the result of a bitwise OR operation on these two constants:
text, rect = font.render("TEXT", (255, 255, 255), style=pygame.freetype.STYLE_STRONG | pygame.freetype.STYLE_ITALIC)
This will create text that is both bold and italic.
I want to be able to use bold or italic styles while rendering with pygame.freetype.Font
, but it turns out freetype.Font.render
doesen’t take any bold
or italic
keyword arguments.
Instead it takes a style
argument. I’m not sure how to use this though, please help me.
import pygame
from pygame import freetype
freetype.init()
font = freetype.Font(“fontfile.ttf”,50)
win = pygame.display.set_mode((500,500))
while True:
for e in pygame.event.get():
if e.type == pygame.quit():
quit()
text,rect = font.render(“TEXT”,(255,255,255),style=?????)
win.fill((0,0,0))
win.blit(text,rect)
pygame.display.update()
- to create bold text you can pass the
pygame.freetype.STYLE_STRONG
constant:
text, rect = font.render("TEXT", (255, 255, 255), style=pygame.freetype.STYLE_STRONG)
- To create italic text you can pass the
pygame.freetype.STYLE_ITALIC
constant:
text, rect = font.render("TEXT", (255, 255, 255), style=pygame.freetype.STYLE_ITALIC)
- If you want to create text that is both bold and italic, you can pass the result of a bitwise OR operation on these two constants:
text, rect = font.render("TEXT", (255, 255, 255), style=pygame.freetype.STYLE_STRONG | pygame.freetype.STYLE_ITALIC)
This will create text that is both bold and italic.