Fun Infused Games  |   Smooth Operator  |   Evil Scale  |   Starcraft Live  |   Wellplayed.net RSS Feed Available RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About

Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Draw Horizontally Centered Text in XNA
Date 3/28/2009    Tags XNA    (2)

Because there is no built in function for centering text drawn to the screen in XNA, I have created a small static method that will take in a string and draw it centered horizontally on the screen.

You will need to pass this method a few parameters so that it can figure out how to center the text and then draw your text (this is why you must pass it spriteBatch for instance). I've also allowed you to specify the color to draw your text and its vertical position.


/// <summary>
/// Take a string and draw it centered horizontally on the screen.
/// </summary>
public static void DrawCentered(String myText, int ScreenWidth, int yPosition, Color drawColor, SpriteFont spriteFont, SpriteBatch spriteBatch)
{
    // Get the size the spritefont will be drawn on the screen.
    Vector2 textSize = spriteFont.MeasureString(myText);

    // Get the position we need to draw the text at for it to be centered.
    int centerXPosition = (ScreenWidth / 2) - ((int)textSize.X / 2);

    // Draw the centered text.
    spriteBatch.DrawString(spriteFont, myText, new Vector2(centerXPosition, yPosition), drawColor);
}
It wouldn't be hard to modify this if you wanted to draw text centered horizontally too or to add an effect like a drop-shadow (draw black text slightly offset from the normal text).

kick it on GameDevKicks.com
/tds/go.php?sid=1" width=

Wellplayed.net
This article has been view 48 times.

Advertisement:

Comments

Mort8088

Avatar

5/15/2009 4:20:34 PM

Dude,

I like your stuff and tweaked this method over at my site check it out. src="h
Kris

Avatar

5/16/2009 10:47:24 PM

Thanks! I found this method quite useful too. You definately cleaned up some things that I missed (expecially passing in height/width when I could just determine that from the spritebatch).

For anyone else reading this, check out Mort's article for a cleaned up version of this...

http://blackhatsoftware.co.uk/?p=255is


Add Comments

Current disabled. Check back soon!
top
top


top
Tags
ASP.net (17)  Annoyances (4)  Video Games (6)  Sage-Like Advice (12)  Domain Name (1)  Internet (5)  NFL (2)  Writing (1)  Visual Studio (1)  Hypership (12)  Site News (2)  Xbox (1)  C# (15)  Sage-Like Advise (1)  Education (1)  Tech Support (1)  MSSQL (1)  Absurd (1)  Abduction Action! (27)  Nasty (36)  Economy (1)  Cool (2)  Sports (11)  .Net (1)  Web Tools (2)  Abduction! (1)  Rant (40)  XBLIG (20)  Tutorial (2)  Nastier (1)  Books (1)  Realty (1)  Programming (3)  Weird (1)  Vista (1)  Development (14)  Design (3)  XNA (35)  Links (1)  Security (2)  JavaScript (7)  CSS (1)  Video (2)  Web Development (1)  Computing (1)  Abudction Action! (1)  Popularity (1)  Game Design (4)  Google (1)  AJAX (4)  Game Development (8)  
top

top
top


Twitter

    Follow me on Twitter



    Buy My Games:
    Abduction Action!
    Nasty

    I'm with Coco.