zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    “问答回复模块”Java开发文档官方改进版讲解【在线实习·吾研第二期】
    “付费邀请模块”产品原型图评审【在线实习·吾研第三期】
    “学长学姐认证模块”测试用例官方改进版讲解【在线实习·吾研第一期】
    “学长认证模块”Java代码2.0官方版要点讲解【在线实习·吾研第一期】
    “问答评论模块”UI作品评审【在线实习·吾研第二期】
    “认证模块”前端代码1.0评审【在线实习·吾研第一期】
    <<中国专利法详解>>学习笔记(一)
    JS 前端获得时间
    北漂的程序员
    Spring类注入异常
  • 原文地址:https://www.cnblogs.com/pmsl/p/7640548.html
Copyright © 2011-2022 走看看