public Font GetFont(string name) { if (!assets.ContainsKey(name)) { assets[name] = new Font(content.Load<SpriteFont>("Content/" + name)); } return assets[name] as Font; }
public Vector2 MeasureText(Font font, string text) { return font.Measure(text); }
public void DrawText(string font, string text, Vector2 position, Color? color = null, float rotation = 0f, Vector2? origin = null, float scale = 1f, Font.Alignment alignment = Font.Alignment.TopLeft) { BeginSpriteBatch(); DrawText(Assets.GetFont(font), text, position, color, rotation, origin, scale, alignment); }
//Methods to draw text. public void DrawText(Font font, string text, Vector2 position, Color? color = null, float rotation = 0f, Vector2? origin = null, float scale = 1f, Font.Alignment alignment = Font.Alignment.TopLeft) { BeginSpriteBatch(); Vector2 finalOrigin = origin ?? new Vector2(0f, 0f); //Apply the alignment. if (alignment != Font.Alignment.TopLeft) { Vector2 textMeasure = font.Measure(text); if (alignment == Font.Alignment.MiddleLeft || alignment == Font.Alignment.MiddleCenter || alignment == Font.Alignment.MiddleRight) finalOrigin.Y += textMeasure.Y / 2; if (alignment == Font.Alignment.BottomLeft || alignment == Font.Alignment.BottomCenter || alignment == Font.Alignment.BottomRight) finalOrigin.Y += textMeasure.Y; if (alignment == Font.Alignment.TopCenter || alignment == Font.Alignment.MiddleCenter || alignment == Font.Alignment.BottomCenter) finalOrigin.X += textMeasure.X / 2; if (alignment == Font.Alignment.TopRight || alignment == Font.Alignment.MiddleRight || alignment == Font.Alignment.BottomRight) finalOrigin.X += textMeasure.X; } spriteBatch.DrawString(font.SpriteFont, text, position, color ?? Color.Black, rotation, finalOrigin, scale, SpriteEffects.None, 0f); }