コード例 #1
0
 public static void DrawTextBottomRightAligned(String fontName, String text, SpriteBatch spriteBatch, Point location, Color color)
 {
     Fonts.DrawTextAligned(fontName, text, spriteBatch, location, TextVerticalAlignment.Bottom, TextHorizantialAlignment.Right, color);
 }
コード例 #2
0
        /// <summary>
        /// Break text up into separate lines to make it fit.
        /// </summary>
        /// <param name="text">The text to be broken up.</param>
        /// <param name="font">The font used ot measure the width of the text.</param>
        /// <param name="rowWidth">The maximum width of each line, in pixels.</param>
        public static List <string> BreakTextIntoList(String fontName, string text, int rowWidth)
        {
            // NOTE: Not my code: Copied from Microsoft Code, from MonoGame Sample Solution, RolePlayingGame Project, Fonts.cs File
            // check parameters
            SpriteFont font = Fonts.GetFont(fontName);

            if (rowWidth <= 0)
            {
                throw new ArgumentOutOfRangeException("rowWidth");
            }

            // create the list
            List <string> lines = new List <string>();

            // check for trivial text
            if (String.IsNullOrEmpty("text"))
            {
                lines.Add(String.Empty);
                return(lines);
            }

            // check for text that fits on a single line
            if (font.MeasureString(text).X <= rowWidth)
            {
                lines.Add(text);
                return(lines);
            }

            // break the text up into words
            string[] words = text.Split(' ');

            // add words until they go over the length
            int currentWord = 0;

            while (currentWord < words.Length)
            {
                int    wordsThisLine = 0;
                string line          = String.Empty;
                while (currentWord < words.Length)
                {
                    string testLine = line;
                    if (testLine.Length < 1)
                    {
                        testLine += words[currentWord];
                    }
                    else if ((testLine[testLine.Length - 1] == '.') ||
                             (testLine[testLine.Length - 1] == '?') ||
                             (testLine[testLine.Length - 1] == '!'))
                    {
                        testLine += "  " + words[currentWord];
                    }
                    else
                    {
                        testLine += " " + words[currentWord];
                    }
                    if ((wordsThisLine > 0) &&
                        (font.MeasureString(testLine).X > rowWidth))
                    {
                        break;
                    }
                    line = testLine;
                    wordsThisLine++;
                    currentWord++;
                }
                lines.Add(line);
            }
            return(lines);
        }
コード例 #3
0
 public static void DrawTextMiddleCenterAligned(String fontName, String text, SpriteBatch spriteBatch, Point location, Color color)
 {
     Fonts.DrawTextAligned(fontName, text, spriteBatch, location, TextVerticalAlignment.Middle, TextHorizantialAlignment.Center, color);
 }
コード例 #4
0
 public static void DrawTextTopLeftAligned(String fontName, String text, SpriteBatch spriteBatch, Point location, Color color)
 {
     Fonts.DrawTextAligned(fontName, text, spriteBatch, location, TextVerticalAlignment.Top, TextHorizantialAlignment.Left, color);
 }
コード例 #5
0
 public static void DrawTextBottomCenterAligned(String fontName, String text, SpriteBatch spriteBatch, Vector2 location, Color color)
 {
     Fonts.DrawTextAligned(fontName, text, spriteBatch, location, TextVerticalAlignment.Bottom, TextHorizantialAlignment.Center, color);
 }
コード例 #6
0
 public static void DrawTextMiddleRightAligned(String fontName, String text, SpriteBatch spriteBatch, Vector2 location, Color color)
 {
     Fonts.DrawTextAligned(fontName, text, spriteBatch, location, TextVerticalAlignment.Middle, TextHorizantialAlignment.Right, color);
 }
コード例 #7
0
 public static void DrawTextAligned(String fontName, String text, SpriteBatch spriteBatch, Point location, TextVerticalAlignment verticalAlignment, TextHorizantialAlignment horizantialAlignment, Color color)
 {
     Fonts.DrawTextAligned(fontName, text, spriteBatch, new Vector2((float)location.X, (float)location.Y), verticalAlignment, horizantialAlignment, color);
 }