Search code examples
c#textxnaalignmentdrawstring

How to align text drawn by SpriteBatch.DrawString?


Is there an easy way to align text to the right and center (instead of default left)?


Solution

  • The first step is to measure the string using SpriteFont.MeasureString().

    Then, for example if you want to draw it to the left of a certain point, instead of to the right as is the default, then you need to subtract the X width of the measurement from the text drawing origin. If you want it to be centered, then you can use half the measurement, etc.