/// <summary> /// Draws the specified text within the specified bounds using the specified /// glyph batch, font, color, back color, and formatting instructions. /// </summary> /// <param name="glyphBatch">The glyph batch in which to draw the text.</param> /// <param name="text">The text to draw.</param> /// <param name="font">The Font to apply to the drawn text.</param> /// <param name="bounds">The Rectangle that represents the bounds of the text.</param> /// <param name="color">The Color to apply to the drawn text.</param> /// <param name="background">The Color to apply to the background area of the drawn text.</param> /// <param name="flags">A bitwise combination of the TextFormatFlags values.</param> public static void DrawText(GlyphBatch glyphBatch, String text, Font font, TextRectangle bounds, Color color, Color background, TextFormatFlags flags) { Vector2 size; TextRun[] textRuns = BreakText(text, font, bounds.Size, flags, out size); Vector2 offset = bounds.Position; // Apply right/bottom/center alignments only when a bounding // rectangle is specified if (bounds.HasSize == true) { if ((flags & TextFormatFlags.Right) != TextFormatFlags.Default) { offset.X = bounds.Right; } else if ((flags & TextFormatFlags.HorizontalCenter) != TextFormatFlags.Default) { offset.X += (float)Math.Round(0.5f * bounds.Width); } // Bottom align only when the text is a single line (only to // be consistent with System.Windows.Forms.TextRenderer) if ((flags & TextFormatFlags.SingleLine) != TextFormatFlags.Default && (flags & TextFormatFlags.Bottom) != TextFormatFlags.Default) { offset.Y = bounds.Bottom - font.Height; } else if ((flags & TextFormatFlags.VerticalCenter) != TextFormatFlags.Default) { offset.Y += (float)Math.Round(0.5f * (bounds.Height - size.Y)); } } foreach (TextRun textRun in textRuns) { Vector2 textRunOffset = offset + textRun.Bounds.Position; if ((flags & TextFormatFlags.Right) != TextFormatFlags.Default) { textRunOffset.X -= textRun.Bounds.Width; } else if ((flags & TextFormatFlags.HorizontalCenter) != TextFormatFlags.Default) { textRunOffset.X -= (float)Math.Round(0.5f * textRun.Bounds.Width); } glyphBatch.DrawBackground(textRunOffset, textRun.Bounds.Size, background); foreach (PositionedGlyph glyph in textRun.Glyphs) { Vector2 pos = textRunOffset + glyph.Position; glyphBatch.Draw(pos, glyph.Glyph.Bounds, color); } } }
/// <summary> /// Draws the specified text at the specified location using the specified /// glyph batch, font, color, back color, and formatting instructions /// </summary> /// <param name="glyphBatch">The glyph batch in which to draw the text.</param> /// <param name="text">The text to draw.</param> /// <param name="font">The Font to apply to the drawn text.</param> /// <param name="position">The Vector2 that represents the upper-left corner of the drawn text.</param> /// <param name="color">The Color to apply to the drawn text.</param> /// <param name="background">The Color to apply to the background area of the drawn text.</param> /// <param name="flags">A bitwise combination of the TextFormatFlags values.</param> public static void DrawText(GlyphBatch glyphBatch, String text, Font font, Vector2 position, Color color, Color background, TextFormatFlags flags) { DrawText(glyphBatch, text, font, new TextRectangle(position), color, background, flags); }
/// <summary> /// Draws the specified text at the specified location using the specified /// glyph batch, font, color, and formatting instructions. /// </summary> /// <param name="glyphBatch">The glyph batch in which to draw the text.</param> /// <param name="text">The text to draw.</param> /// <param name="font">The Font to apply to the drawn text.</param> /// <param name="position">The Vector2 that represents the upper-left corner of the drawn text.</param> /// <param name="color">The Color to apply to the drawn text.</param> /// <param name="flags">A bitwise combination of the TextFormatFlags values.</param> public static void DrawText(GlyphBatch glyphBatch, String text, Font font, Vector2 position, Color color, TextFormatFlags flags) { DrawText(glyphBatch, text, font, new TextRectangle(position), color, Color.TransparentWhite, flags); }
/// <summary> /// Draws the specified text within the specified bounds using the specified /// glyph batch, font, color, and formatting instructions. /// </summary> /// <param name="glyphBatch">The glyph batch in which to draw the text.</param> /// <param name="text">The text to draw.</param> /// <param name="font">The Font to apply to the drawn text.</param> /// <param name="bounds">The Rectangle that represents the bounds of the text.</param> /// <param name="color">The Color to apply to the drawn text.</param> /// <param name="flags">A bitwise combination of the TextFormatFlags values.</param> public static void DrawText(GlyphBatch glyphBatch, String text, Font font, TextRectangle bounds, Color color, TextFormatFlags flags) { DrawText(glyphBatch, text, font, bounds, color, Color.TransparentWhite, flags); }
/// <summary> /// Draws the specified text within the specified bounds using the specified /// glyph batch, font, color, and back color. /// </summary> /// <param name="glyphBatch">The glyph batch in which to draw the text.</param> /// <param name="text">The text to draw.</param> /// <param name="font">The Font to apply to the drawn text.</param> /// <param name="bounds">The Rectangle that represents the bounds of the text.</param> /// <param name="color">The Color to apply to the drawn text.</param> /// <param name="background">The Color to apply to the background area of the drawn text.</param> public static void DrawText(GlyphBatch glyphBatch, String text, Font font, TextRectangle bounds, Color color, Color background) { DrawText(glyphBatch, text, font, bounds, color, background, TextFormatFlags.Default); }