Example #1
0
        public static void DrawTextAligned(String fontName, String text, SpriteBatch spriteBatch, Vector2 location, TextVerticalAlignment verticalAlignment, TextHorizantialAlignment horizantialAlignment, Color color)
        {
            SpriteFont spriteFont = Fonts.GetFont(fontName);
            Vector2    textSize   = spriteFont.MeasureString(text);

            Vector2 alignedLocation = new Vector2();

            // Calculate Horizantial Aligned Location
            switch (horizantialAlignment)
            {
            case TextHorizantialAlignment.Left:
                alignedLocation.X = location.X;
                break;

            case TextHorizantialAlignment.Center:
                alignedLocation.X = location.X - (textSize.X / 2.0f);
                break;

            case TextHorizantialAlignment.Right:
                alignedLocation.X = location.X - textSize.X;
                break;
            }

            // Calculate Vertical Aligned Location
            switch (verticalAlignment)
            {
            case TextVerticalAlignment.Top:
                alignedLocation.Y = location.Y;
                break;

            case TextVerticalAlignment.Middle:
                alignedLocation.Y = location.Y - (textSize.Y / 2.0f);
                break;

            case TextVerticalAlignment.Bottom:
                alignedLocation.Y = location.Y - textSize.Y;
                break;
            }

            spriteBatch.DrawString(spriteFont, text, alignedLocation, color);
        }
Example #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);
        }