Untiy:Text文本超长的时候省略展示

public static class TextExtension
{
    /// <summary>
    /// 设置文本长度
    /// </summary>
    /// <param name="text"></param>
    /// <param name="maxWidth"></param>
    /// <param name="input">输入框</param>
    /// <param name="suffix"></param>
    public static void SetTextWithEllipsis(this Text text, int maxWidth, InputField input = null, string suffix = "...")
    {
        int textLeng = GetTextLeng(text);

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

    /// <summary>
    /// 中间添加省略号
    /// </summary>
    /// <param name="text"></param>
    /// <param name="maxWidth"></param>
    /// <param name="suffix"></param>
    public static void SetTextWithMiddle(this Text text, int maxWidth, string suffix = "...")
    {
        int textLeng = GetTextLeng(text);

        if (textLeng > maxWidth)
        {
            int suffixLeng = GetTextLeng(text, suffix);
            int middle = Mathf.Abs((maxWidth - suffixLeng)/2);
            string startStr = StripLength(text, middle);
            string endStr = StripEndLength(text, middle);
            text.text = startStr + suffix + endStr;
        }
        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>
    /// 按照指定宽度截断字符串
    /// </summary>
    /// <param name="text"></param>
    /// <param name="width"></param>
    /// <returns></returns>
    private static string StripEndLength(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 -1; i >= 0; 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.Remove(0,i);
    }

    /// <summary>
    /// 获取字符串在text中的长度
    /// </summary>
    /// <param name="text"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    public static int GetTextLeng(this 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;
    }

    /// <summary>
    /// 文本长度是否超出
    /// </summary>
    /// <param name="text"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool OutLength(this Text text, int maxWidth)
    {
        int textLeng = GetTextLeng(text);

        if (textLeng > maxWidth)
        {
            return true;
        }
        return false;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾荒的小海螺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值