码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 中给定宽度截断text内容

时间:2017-10-09 14:08:54      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:get   ext   extend   pre   advance   character   代码   lte   取字符串   

在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内容

标签:get   ext   extend   pre   advance   character   代码   lte   取字符串   

原文地址:http://www.cnblogs.com/pmsl/p/7640548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!