UIFont.CalculatePrintedSize C# (CSharp) Méthode

CalculatePrintedSize() public méthode

Get the printed size of the specified string. The returned value is in local coordinates. Multiply by transform's scale to get pixels.
public CalculatePrintedSize ( string text, bool encoding, SymbolStyle symbolStyle ) : Vector2
text string
encoding bool
symbolStyle SymbolStyle
Résultat Vector2
    public Vector2 CalculatePrintedSize(string text, bool encoding, SymbolStyle symbolStyle)
    {
        if (mReplacement != null) return mReplacement.CalculatePrintedSize(text, encoding, symbolStyle);

        Vector2 v = Vector2.zero;

        if (mFont != null && mFont.isValid && !string.IsNullOrEmpty(text))
        {
            if (encoding) text = NGUITools.StripSymbols(text);

            int length = text.Length;
            int maxX = 0;
            int x = 0;
            int y = 0;
            int prev = 0;
            int lineHeight = (mFont.charSize + mSpacingY);
            bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols;

            for (int i = 0; i < length; ++i)
            {
                char c = text[i];

                // Start a new line
                if (c == '\n')
                {
                    if (x > maxX) maxX = x;
                    x = 0;
                    y += lineHeight;
                    prev = 0;
                    continue;
                }

                // Skip invalid characters
                if (c < ' ') { prev = 0; continue; }

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

                if (symbol == null)
                {
                    // Get the glyph for this character
                    BMGlyph glyph = mFont.GetGlyph(c);

                    if (glyph != null)
                    {
                        x += mSpacingX + ((prev != 0) ? glyph.advance + glyph.GetKerning(prev) : glyph.advance);
                        prev = c;
                    }
                }
                else
                {
                    // Symbol found -- use it
                    x += mSpacingX + symbol.advance;
                    i += symbol.length - 1;
                    prev = 0;
                }
            }

            // Convert from pixel coordinates to local coordinates
            float scale = (mFont.charSize > 0) ? 1f / mFont.charSize : 1f;
            v.x = scale * ((x > maxX) ? x : maxX);
            v.y = scale * (y + lineHeight);
        }
        return v;
    }

Usage Example

    void CalculateEmotionSpace(ref string text, out Vector2 LeftSideSpace, out int nNextLineNum, out bool needShow)
    {
        nNextLineNum  = 0;
        LeftSideSpace = Vector2.zero;
        needShow      = true;

        if (null == m_Font)
        {
            m_Font = m_ChatText.font;
            if (m_Font != null)
            {
                m_ChatTextHeight = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.None).y;
                SPACE_WIDTH      = m_Font.CalculatePrintedSize(" ", true, UIFont.SymbolStyle.Uncolored).x;
            }
        }

        string striptext = NGUITools.StripSymbols(text);

        int stripEmotionStart = striptext.IndexOf("[em=");

        LeftSideSpace = m_Font.CalculatePrintedSize(striptext.Substring(0, stripEmotionStart), true, UIFont.SymbolStyle.None);
//        float fEllipsisWidth = m_Font.CalculatePrintedSize("...", true, UIFont.SymbolStyle.None).x;
//         if (LeftSideSpace.x + EMOTIONITEM_WIDTH > labelChatText.GetComponent<UIWidget>().width * MaxLines - fEllipsisWidth)
//         {
//             needShow = false;
//             return;
//         }

        int nLineStart = 0;

        for (int i = nLineStart + 1; i <= stripEmotionStart; i++)
        {
            float fChatWidth = m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart), true, UIFont.SymbolStyle.None).x;
            if (i == stripEmotionStart)
            {
                if (fChatWidth > m_ChatText.width)
                {
                    nNextLineNum    += 1;
                    LeftSideSpace.x -= m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart - 1), true, UIFont.SymbolStyle.Uncolored).x;
                    nLineStart       = i - 1;
                }
                else if (fChatWidth + SPACE_WIDTH > m_ChatText.width)
                {
                    nNextLineNum   += 1;
                    LeftSideSpace.x = 0;
                }
            }
            else
            {
                if (fChatWidth > m_ChatText.width)
                {
                    nNextLineNum    += 1;
                    LeftSideSpace.x -= m_Font.CalculatePrintedSize(striptext.Substring(nLineStart, i - nLineStart - 1), true, UIFont.SymbolStyle.Uncolored).x;
                    nLineStart       = i - 1;
                }
            }
        }
    }
All Usage Examples Of UIFont::CalculatePrintedSize