UIFont.WrapText C# (CSharp) Method

WrapText() public method

Text wrapping functionality. The 'maxWidth' should be in local coordinates (take pixels and divide them by transform's scale).
public WrapText ( string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle ) : string
text string
maxWidth float
maxLineCount int
encoding bool
symbolStyle SymbolStyle
return string
    public string WrapText(string text, float maxWidth, int maxLineCount, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null) return mReplacement.WrapText(text, maxWidth, maxLineCount, encoding, symbolStyle);

        // Width of the line in pixels
        int lineWidth = Mathf.RoundToInt(maxWidth * size);
        if (lineWidth < 1) return text;

        StringBuilder sb = new StringBuilder();
        int textLength = text.Length;
        int remainingWidth = lineWidth;
        int previousChar = 0;
        int start = 0;
        int offset = 0;
        bool lineIsEmpty = true;
        bool multiline = (maxLineCount != 1);
        int lineCount = 1;
        bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols;

        // Run through all characters
        for (; offset < textLength; ++offset)
        {
            char ch = text[offset];

            // New line character -- start a new line
            if (ch == '\n')
            {
                if (!multiline || lineCount == maxLineCount ) break;
                remainingWidth = lineWidth;

                // Add the previous word to the final string
                if (start < offset) sb.Append(text.Substring(start, offset - start + 1));
                else sb.Append(ch);

                lineIsEmpty = true;
                ++lineCount;
                start = offset + 1;
                previousChar = 0;
                continue;
            }

            // If this marks the end of a word, add it to the final string.
            if (ch == ' ' && previousChar != ' ' && start < offset)
            {
                sb.Append(text.Substring(start, offset - start + 1));
                lineIsEmpty = false;
                start = offset + 1;
                previousChar = ch;
            }

            // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
            if (encoding && ch == '[')
            {
                if (offset + 2 < textLength)
                {
                    if (text[offset + 1] == '-' && text[offset + 2] == ']')
                    {
                        offset += 2;
                        continue;
                    }
                    else if (offset + 7 < textLength && text[offset + 7] == ']')
                    {
                        if (NGUITools.EncodeColor(NGUITools.ParseColor(text, offset + 1)) == text.Substring(offset + 1, 6).ToUpper())
                        {
                            offset += 7;
                            continue;
                        }
                    }
                }
            }

            // See if there is a symbol matching this text
            BMSymbol symbol = useSymbols ? MatchSymbol(text, offset, textLength) : null;

            // Calculate how wide this symbol or character is going to be
            int glyphWidth = mSpacingX;

            if (symbol != null)
            {
                glyphWidth += symbol.advance;
            }
            else
            {
                // Find the glyph for this character
                BMGlyph glyph = (symbol == null) ? mFont.GetGlyph(ch) : null;

                if (glyph != null)
                {
                    glyphWidth += (previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance;
                }
                else continue;
            }

            // Remaining width after this glyph gets printed
            remainingWidth -= glyphWidth;

            // Doesn't fit?
            if (remainingWidth < 0)
            {
                // Can't start a new line
                if (lineIsEmpty || !multiline || lineCount == maxLineCount)
                {
                    // This is the first word on the line -- add it up to the character that fits
                    sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

                    if (!multiline || lineCount == maxLineCount)
                    {
                        start = offset;
                        break;
                    }
                    EndLine(ref sb);

                    // Start a brand-new line
                    lineIsEmpty = true;
                    ++lineCount;

                    if (ch == ' ')
                    {
                        start = offset + 1;
                        remainingWidth = lineWidth;
                    }
                    else
                    {
                        start = offset;
                        remainingWidth = lineWidth - glyphWidth;
                    }
                    previousChar = 0;
                }
                else
                {
                    // Skip all spaces before the word
                    while (start < textLength && text[start] == ' ') ++start;

                    // Revert the position to the beginning of the word and reset the line
                    lineIsEmpty = true;
                    remainingWidth = lineWidth;
                    offset = start - 1;
                    previousChar = 0;
                    if (!multiline || lineCount == maxLineCount) break;
                    ++lineCount;
                    EndLine(ref sb);
                    continue;
                }
            }
            else previousChar = ch;

            // Advance the offset past the symbol
            if (symbol != null)
            {
                offset += symbol.length - 1;
                previousChar = 0;
            }
        }

        if (start < offset) sb.Append(text.Substring(start, offset - start));
        return sb.ToString();
    }

Same methods

UIFont::WrapText ( string text, float maxWidth, int maxLineCount ) : string
UIFont::WrapText ( string text, float maxWidth, int maxLineCount, bool encoding ) : string

Usage Example

コード例 #1
0
    /// <summary>
    /// Process the raw text, called when something changes.
    /// </summary>

    void ProcessText()
    {
        mChanged   = true;
        hasChanged = false;
        mLastText  = mText;

        mProcessedText = mText;
        if (TranslateReturn)
        {
            mProcessedText = mText.Replace("\\n", "\n");
        }

        if (mPassword)
        {
            string hidden = "";

            if (mShowLastChar)
            {
                for (int i = 0, imax = mProcessedText.Length - 1; i < imax; ++i)
                {
                    hidden += "*";
                }
                if (mProcessedText.Length > 0)
                {
                    hidden += mProcessedText[mProcessedText.Length - 1];
                }
            }
            else
            {
                for (int i = 0, imax = mProcessedText.Length; i < imax; ++i)
                {
                    hidden += "*";
                }
            }
            mProcessedText = mFont.WrapText(hidden, mMaxLineWidth / cachedTransform.localScale.x,
                                            mMaxLineCount, false, UIFont.SymbolStyle.None);
        }
        else if (mMaxLineWidth > 0)
        {
            mProcessedText = mFont.WrapText(mProcessedText, mMaxLineWidth / cachedTransform.localScale.x, mMaxLineCount, mEncoding, mSymbols, mSpacingX);
        }
        else if (mMaxLineCount > 0)
        {
            mProcessedText = mFont.WrapText(mProcessedText, 100000f, mMaxLineCount, mEncoding, mSymbols, mSpacingX);
        }

        mSize = !string.IsNullOrEmpty(mProcessedText) ? mFont.CalculatePrintedSize(mProcessedText, mEncoding, mSymbols, mSpacingX, mSpacingY, TranslateReturn) : Vector2.one;
        float scale = cachedTransform.localScale.x;

        mSize.x = Mathf.Max(mSize.x, (mFont != null && scale > 1f) ? lineWidth / scale : 1f);
        mSize.y = Mathf.Max(mSize.y, 1f);
    }
All Usage Examples Of UIFont::WrapText