6
\$\begingroup\$

I am trying to position some text on my screen using pygame, and knowing the size of the string would help.

When using C# and XNA I could use SpriteFont.MeasureString("Hello World") to get the size of the rendered string.

Is there any way to do something similar in pygame?

\$\endgroup\$
6
\$\begingroup\$

You can use the size method

size()
size(text) -> (width, height)

Returns the dimensions needed to render the text. This can be used to help determine the positioning needed for text before it isrendered. It can also be used for wordwrapping and other layout effects.

Here is an example

myFont = pygame.font.Font(None, fontSize)

width  = myFont.size("Hello World")[0]
heigth = myFont.size("Hello World")[1]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.