SpriteBatch.DrawString Method (SpriteFont, String, Vector2, Color)

Adds a sprite string to the batch of sprites to be rendered, specifying the font, output text, screen position, and color tint.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

Syntax

public void DrawString (
         SpriteFont spriteFont,
         string text,
         Vector2 position,
         Color color
)

Parameters

  • spriteFont
    The sprite font.
  • text
    The string to draw.
  • position
    The location, in screen coordinates, where the text will be drawn.
  • color
    The desired color of the text.

Exceptions

Exception type Condition
ArgumentException There is a character in text that was not imported by the SpriteFont. You must include all the characters in the character regions in the sprite font file. For more information, see How to: Extend the Font Description Processor to Support Additional Characters.
ArgumentNullException spriteFont or text is null.
InvalidOperationException DrawString was called, but Begin has not yet been called. Begin must be called successfully before DrawString can be called.

Remarks

SpriteFont objects are loaded from the Content Manager. See the SpriteFont class for more information.

Before any calls to DrawString, you must call Begin. Once all calls to DrawString are complete, call End.

Use a newline character (\n) to draw more than one line of text.

See Also

Tasks

How to: Draw Text

Concepts

2D Graphics Overview

Reference

SpriteBatch Class
SpriteBatch Members
Microsoft.Xna.Framework.Graphics Namespace

Platforms

Xbox 360, Windows XP SP2, Windows Vista