Table of Contents

Class SpriteFont

Namespace
Microsoft.Xna.Framework.Graphics
Assembly
MonoGame.Framework.dll
public sealed class SpriteFont
Inheritance
SpriteFont
Inherited Members

Constructors

SpriteFont(Texture2D, List<Rectangle>, List<Rectangle>, List<char>, int, float, List<Vector3>, char?)

Initializes a new instance of the SpriteFont class.

public SpriteFont(Texture2D texture, List<Rectangle> glyphBounds, List<Rectangle> cropping, List<char> characters, int lineSpacing, float spacing, List<Vector3> kerning, char? defaultCharacter)

Parameters

texture Texture2D

The font texture.

glyphBounds List<Rectangle>

The rectangles in the font texture containing letters.

cropping List<Rectangle>

The cropping rectangles, which are applied to the corresponding glyphBounds to calculate the bounds of the actual character.

characters List<char>

The characters.

lineSpacing int

The line spacing (the distance from baseline to baseline) of the font.

spacing float

The spacing (tracking) between characters in the font.

kerning List<Vector3>

The letters kernings (X - left side bearing, Y - width and Z - right side bearing).

defaultCharacter char?

The character that will be substituted when a given character is not included in the font.

Properties

Characters

Gets a collection of the characters in the font.

public ReadOnlyCollection<char> Characters { get; }

Property Value

ReadOnlyCollection<char>

DefaultCharacter

Gets or sets the character that will be substituted when a given character is not included in the font.

public char? DefaultCharacter { get; set; }

Property Value

char?

Glyphs

All the glyphs in this SpriteFont.

public SpriteFont.Glyph[] Glyphs { get; }

Property Value

Glyph[]

LineSpacing

Gets or sets the line spacing (the distance from baseline to baseline) of the font.

public int LineSpacing { get; set; }

Property Value

int

Spacing

Gets or sets the spacing (tracking) between characters in the font.

public float Spacing { get; set; }

Property Value

float

Texture

Gets the texture that this SpriteFont draws from.

public Texture2D Texture { get; }

Property Value

Texture2D

Remarks

Can be used to implement custom rendering of a SpriteFont

Methods

GetGlyphs()

Returns a copy of the dictionary containing the glyphs in this SpriteFont.

public Dictionary<char, SpriteFont.Glyph> GetGlyphs()

Returns

Dictionary<char, SpriteFont.Glyph>

A new Dictionary containing all of the glyphs in this SpriteFont

Remarks

Can be used to calculate character bounds when implementing custom SpriteFont rendering.

MeasureString(string)

Returns the size of a string when rendered in this font.

public Vector2 MeasureString(string text)

Parameters

text string

The text to measure.

Returns

Vector2

The size, in pixels, of 'text' when rendered in this font.

MeasureString(StringBuilder)

Returns the size of the contents of a StringBuilder when rendered in this font.

public Vector2 MeasureString(StringBuilder text)

Parameters

text StringBuilder

The text to measure.

Returns

Vector2

The size, in pixels, of 'text' when rendered in this font.