コード例 #1
0
        private TextBlockPlus CreateTextBlock(string text)
        {
            TextBlockPlus tb = new TextBlockPlus();

            tb.VerticalAlignment = VerticalAlignment.Bottom;
            tb.Text = text;
            tb.Tag  = new RichTextTag(RichTextBlock.DefaultStyle);

            return(tb);
        }
コード例 #2
0
        /// <summary>
        /// Splits a provided text string and returns a collection
        /// </summary>
        /// <param name="template">A textblock template used to determine the dimensions of the characters</param>
        /// <param name="text">The text string to split</param>
        /// <param name="x">A starting x position</param>
        /// <param name="width">A width to split the text on</param>
        /// <returns>A list of text strings</returns>
        private List <string> SplitText(TextBlockPlus template, string text, double x, double width)
        {
            List <string> results         = new List <string>();
            string        temp            = template.Text;
            double        currentX        = x;
            int           lineIndex       = 0;
            int           indexOfLastWord = 0;
            int           charsRemaining  = text.Length;
            int           i = 0;
            char          c;

            if (text.Length < 2 || width < 20)
            {
                results.Add(text);
                return(results);
            }

            while (i < text.Length)
            {
                c = text[i];

                template.Text = c.ToString();
                currentX     += template.ContentWidth;

                if (currentX > width)
                {
                    if (indexOfLastWord == lineIndex)
                    {
                        if (indexOfLastWord == 0 && x > 1)
                        {
                            indexOfLastWord = text.Length;
                            results.Add("");
                        }
                        else
                        {
                            indexOfLastWord = i - 1;
                        }
                    }

                    results.Add(text.Substring(lineIndex, indexOfLastWord - lineIndex));
                    charsRemaining -= indexOfLastWord - lineIndex;

                    if (i != indexOfLastWord)
                    {
                        i = indexOfLastWord;
                    }
                    lineIndex = i;
                    currentX  = 0;
                    continue;
                }

                if (c == ' ' || c == '-')
                {
                    indexOfLastWord = i + 1;
                }
                i++;
            }

            if (charsRemaining > 0)
            {
                results.Add(text.Substring(text.Length - charsRemaining, charsRemaining));
            }

            template.Text = temp;

            /*
             * if (results.Count > 1)
             * {
             *  if (results[0].Length == 0)
             *  {
             *      results.RemoveAt(0);
             *  }
             * }*/

            return(results);
        }