Unity 中给定宽度截断text内容

Posted 熊二

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 中给定宽度截断text内容相关的知识,希望对你有一定的参考价值。

在Unity开发中经常会遇到这样的问题,如果text的内容超过给定的宽度则截断并追加 “...”。

以下便是解决该问题的代码

    /// <summary>
    /// 根据给定的宽度截断字符串。并将给定的后缀拼接到截断后的字符串。
    /// </summary>
    /// <param name="text"></param>
    /// <param name="maxWidth"></param>
    /// <param name="suffix"></param>
    /// <returns></returns>
    public static string StringEllipsis(Text text,int maxWidth,string suffix = "...")
    {
        int textLeng = GetTextLeng(text);

        if(textLeng > maxWidth)
        {
            int suffixLeng = GetTextLeng(text, suffix);
            return StripLength(text, maxWidth - suffixLeng) + suffix;
        }
        else
        {
            return text.text;
        }
    }

    /// <summary>
    /// 按照指定宽度截断字符串
    /// </summary>
    /// <param name="text"></param>
    /// <param name="width"></param>
    /// <returns></returns>
    public static string StripLength(Text text,int width)
    {
        int totalLength = 0;
        Font myFont = text.font; 
        myFont.RequestCharactersInTexture(text.text, text.fontSize, text.fontStyle);
        CharacterInfo characterInfo = new CharacterInfo();

        char[] charArr = text.text.ToCharArray();

        int i = 0;
        for (; i < charArr.Length;i++)
        {
            myFont.GetCharacterInfo(charArr[i], out characterInfo, text.fontSize);

            int newLength = totalLength + characterInfo.advance;
            if (newLength > width)
            {
                if (Mathf.Abs(newLength - width) > Mathf.Abs(width - totalLength))
                {
                    break;
                }
                else
                {
                    totalLength = newLength;
                    break;
                }
            }
            totalLength += characterInfo.advance;
        }
        return text.text.Substring(0, i);
    }

    /// <summary>
    /// 获取字符串在text中的长度
    /// </summary>
    /// <param name="text"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    public static int GetTextLeng(Text text,string str=null)
    {
        Font mFont = text.font;
        string mStr = string.IsNullOrEmpty(str) ? text.text : str;
        mFont.RequestCharactersInTexture(mStr, text.fontSize, text.fontStyle);
        char[] charArr = mStr.ToCharArray();
        int totalTextLeng = 0;
        CharacterInfo character = new CharacterInfo();
        for (int i = 0; i < charArr.Length; i++)
        {
            mFont.GetCharacterInfo(charArr[i], out character, text.fontSize);
            totalTextLeng += character.advance;
        }
        return totalTextLeng;
    }

当然这类方法也可以写成Text的扩展,方便调用

public static class ExtendText
{
    public static void StringEllipsis(this Text text,int maxWidth,string suffix = "...")
    {
        int textLeng = GetTextLeng(text);

        if (textLeng > maxWidth)
        {
            int suffixLeng = GetTextLeng(text, suffix);
            text.text =  StripLength(text, maxWidth - suffixLeng) + suffix;
        }
        else
        {
            return ;
        }
    }

    /// <summary>
    /// 按照指定宽度截断字符串
    /// </summary>
    /// <param name="text"></param>
    /// <param name="width"></param>
    /// <returns></returns>
    private static string StripLength(Text text, int width)
    {
        int totalLength = 0;
        Font myFont = text.font;
        myFont.RequestCharactersInTexture(text.text, text.fontSize, text.fontStyle);
        CharacterInfo characterInfo = new CharacterInfo();

        char[] charArr = text.text.ToCharArray();

        int i = 0;
        for (; i < charArr.Length; i++)
        {
            myFont.GetCharacterInfo(charArr[i], out characterInfo, text.fontSize);

            int newLength = totalLength + characterInfo.advance;
            if (newLength > width)
            {
                if (Mathf.Abs(newLength - width) > Mathf.Abs(width - totalLength))
                {
                    break;
                }
                else
                {
                    totalLength = newLength;
                    break;
                }
            }
            totalLength += characterInfo.advance;
        }
        return text.text.Substring(0, i);
    }

    /// <summary>
    /// 获取字符串在text中的长度
    /// </summary>
    /// <param name="text"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    private static int GetTextLeng(Text text, string str = null)
    {
        Font mFont = text.font;
        string mStr = string.IsNullOrEmpty(str) ? text.text : str;
        mFont.RequestCharactersInTexture(mStr, text.fontSize, text.fontStyle);
        char[] charArr = mStr.ToCharArray();
        int totalTextLeng = 0;
        CharacterInfo character = new CharacterInfo();
        for (int i = 0; i < charArr.Length; i++)
        {
            mFont.GetCharacterInfo(charArr[i], out character, text.fontSize);
            totalTextLeng += character.advance;
        }
        return totalTextLeng;
    }
}

 

以上是关于Unity 中给定宽度截断text内容的主要内容,如果未能解决你的问题,请参考以下文章

如何设置 <td> 宽度以直观地截断其显示的内容?

选择固定宽度的下拉菜单,在 IE 中截断内容

方案集合 之文本溢出截断省略

qml中Text元素三个属性clipelidewrapMode的区别

CSS实现DIV超长截断,并显示...

用 CSS 截断长字符串:可行吗?