/// <summary> /// If the text were written to the specified rectangle, how much width and height would it actually use? /// This method ignores the rectangle's X and Y properties. /// </summary> /// <param name="text">Text to draw.</param> /// <param name="rectangle">Rectangle bounding the text.</param> /// <param name="font">Font to use.</param> public static Rectangle GetMultiLineTextBounds(string text, Rectangle rectangle, FontFamily font = FontFamily.Normal) { Rectangle bounds; List <MultilineFragment> empty; Primitives.SetupNewMultilineString(font, text, rectangle, Color.Black, TextAlignment.TopLeft, out bounds, out empty); return(bounds); }
/// <summary> /// Draws a multiline text using the Primitives spritebatch. /// </summary> /// <param name="text">Text to be drawn.</param> /// <param name="rectangle">The rectangle bounding the text.</param> /// <param name="color">Text color.</param> /// <param name="font">Text font (Verdana 14, if null).</param> /// <param name="alignment">Text alignment.</param> public static void DrawMultiLineText( string text, Rectangle rectangle, Color color, FontFamily font = FontFamily.Small, TextAlignment alignment = TextAlignment.TopLeft) { Rectangle empty; MultilineString ms = new MultilineString(text, rectangle, alignment, font, Vector2.Zero, true, null, color); foreach (MultilineString msCache in multilineStringCache) { if (ms.Equals(msCache)) { foreach (MultilineFragment line in msCache.CachedLines) { if (line.Icon != null) { spriteBatch.Draw(line.Icon, new Rectangle(rectangle.X + (int)line.PositionOffset.X, rectangle.Y + (int)line.PositionOffset.Y, line.IconWidth, line.IconWidth), Color.White); } else { if (line.Color != color) { /* * Color shadowColor = line.Color.Alpha(120); * spriteBatch.DrawString(line.Font, line.Text, * new Vector2(rectangle.X + (int)line.PositionOffset.X - 1, * rectangle.Y + (int)line.PositionOffset.Y - 1), shadowColor); * spriteBatch.DrawString(line.Font, line.Text, * new Vector2(rectangle.X + (int)line.PositionOffset.X + 1, * rectangle.Y + (int)line.PositionOffset.Y - 1), shadowColor); * spriteBatch.DrawString(line.Font, line.Text, * new Vector2(rectangle.X + (int)line.PositionOffset.X - 1, * rectangle.Y + (int)line.PositionOffset.Y + 1), shadowColor); * spriteBatch.DrawString(line.Font, line.Text, * new Vector2(rectangle.X + (int)line.PositionOffset.X + 1, * rectangle.Y + (int)line.PositionOffset.Y + 1), shadowColor); */ } spriteBatch.DrawString(line.Font, line.Text, new Vector2(rectangle.X + (int)line.PositionOffset.X, rectangle.Y + (int)line.PositionOffset.Y), line.Color); } } return; } } List <MultilineFragment> cachedLines; Primitives.SetupNewMultilineString(font, text, rectangle, color, alignment, out empty, out cachedLines); multilineStringCache.Add(new MultilineString(text, rectangle, alignment, font, Vector2.Zero, true, cachedLines, color)); Primitives.DrawMultiLineText(text, rectangle, color, font, alignment); }