【狂云歌之unity_vr】unity里获取text中文字宽度并截断省略

鬼子母
• 阅读 3952

【狂云歌之unity_vr】unity里获取text中文字宽度并截断省略

【狂云歌之unity_vr】unity里获取text中文字宽度并截断省略

前言

 在unity的ugui中Text控件,有时我们会有各种各样的需求,比如类似html中css的text-overflow属性,希望一段文字如果不够长就全显示,如果特别长就截断并且后面加上例如...这种后缀。

 好吧这样的需求在ugui里貌似没有现成的方法,如果有的话麻烦指点一下~

实现

 大概的思路就是

  • 首先要能判断什么时候overflow

  • 并且支持加上后缀

 那么text控件本来是支持overflow然后直接截断的,但是比较暴力,直接砍断,不能加后缀,并不满足我们的需求。

 然后如果简单的通过字符个数截断,那根本不行,如果根据中英文字符来根据长度截断,这个我试过,然而字体并不一定是一个中文相当于俩英文字符,于是乎如果有一大排lllll或者iii什么的,悲剧无以言表。

 所以我们需要知道一段文字所对应的渲染之后的长度。如果从text的preferwidth或者通过添加content size filter组件应该也能完成类似任务,不过我倾向于直接算好长度去填充。

 这个功能核心代码为

Font myFont = text.font;  //chatText is my Text component
myFont.RequestCharactersInTexture(message, text.fontSize, text.fontStyle);
CharacterInfo characterInfo = new CharacterInfo();

char[] arr = message.ToCharArray();

foreach (char c in arr)
{
    myFont.GetCharacterInfo(c, out characterInfo, text.fontSize);

    totalLength += characterInfo.advance;
}

 其中text为Text文本控件,RequestCharactersInTexture主要相当于指定需要渲染哪些字符(然后根据CharacterInfo.characterInfo是可以拿到本次生成的去重后的字符集)。接下来通过myFont.GetCharacterInfo(c, out characterInfo, text.fontSize);分别去获得每个字符的信息,然后characterInfo.advance就拿到了每个字符的渲染长度。

 拿到每个字符长度之后那就简单多了,计算一下需要截断的字符总长度,如果大于限制长度,就除去后缀长度后,截取子字符串,然后再接上后缀。这个事情就搞定了。

效果如下:

【狂云歌之unity_vr】unity里获取text中文字宽度并截断省略

全部如下,这个例子是需要一个text和一个button,点击button,随机生成文字在text上。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TextWidth : MonoBehaviour {
    public Text text;
    public Button button;
    const string suffix = "...";
    const int MAX_WIDTH = 200;
    int suffixWidth = 0;
    string[] seeds = { "是都", "60°", "qの", "【】" , "d a", "as", "WW", "II", "fs", "as", "WW", "II", "fs" };
    // Use this for initialization
    void Start () {
        Init();
        button.onClick.AddListener(Rand);
    }

    void Init()
    {
        //计算后缀的长度
        suffixWidth = CalculateLengthOfText(suffix);
        Debug.Log("suffixWidth : " + suffixWidth);
    }

    string StripLengthWithSuffix(string input, int maxWidth = MAX_WIDTH)
    {
        int len = CalculateLengthOfText(input);
        Debug.Log("input total length = " + len);
        //截断text的长度,如果总长度大于限制的最大长度,
        //那么先根据最大长度减去后缀长度的值拿到字符串,在拼接上后缀
        if (len > maxWidth)
        {
            return StripLength(input, maxWidth - suffixWidth) + suffix;
        }else
        {
            return input;
        }
    }

    //随机生成个字符串
    void Rand()
    {
        int min = 12;
        int max = 16;
        int num = (int)(Random.value * (max - min) + min);

        Debug.Log("-------------------------\n num : " + num);
        string s = "";
        for (int j = 0; j < num; j++)
        {
            int len = seeds.Length;
            int index = (int)(Random.value * (len));
            s += seeds[index];
        }
        Debug.Log("s : " + s);
        text.text = StripLengthWithSuffix(s);
        Debug.Log("StripLength " + text.text);
    }

    /// <summary>
    /// 根据maxWidth来截断input拿到子字符串
    /// </summary>
    /// <param name="input"></param>
    /// <param name="maxWidth"></param>
    /// <returns></returns>
    string StripLength(string input, int maxWidth = MAX_WIDTH)
    {
        int totalLength = 0;
        Font myFont = text.font;  //chatText is my Text component
        
        myFont.RequestCharactersInTexture(input, text.fontSize, text.fontStyle);
        
        CharacterInfo characterInfo = new CharacterInfo();

        char[] arr = input.ToCharArray();

        int i = 0;
        foreach (char c in arr)
        {
            myFont.GetCharacterInfo(c, out characterInfo, text.fontSize);

            int newLength = totalLength + characterInfo.advance;
            if (newLength > maxWidth)
            {
                Debug.LogFormat("newLength {0},  totalLength {1}: ", newLength, totalLength);
                if (Mathf.Abs(newLength - maxWidth) > Mathf.Abs(maxWidth - totalLength)){
                    break;
                }else
                {
                    totalLength = newLength;
                    i++;
                    break;
                }
            }
            totalLength += characterInfo.advance;
            i++;
        }
        Debug.LogFormat("totalLength {0} : ", totalLength);
        return input.Substring(0, i);
    }

    /// <summary>
    /// 计算字符串在指定text控件中的长度
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    int CalculateLengthOfText(string message)
    {
        int totalLength = 0;
        Font myFont = text.font;  //chatText is my Text component
        myFont.RequestCharactersInTexture(message, text.fontSize, text.fontStyle);
        CharacterInfo characterInfo = new CharacterInfo();

        char[] arr = message.ToCharArray();

        foreach (char c in arr)
        {
            myFont.GetCharacterInfo(c, out characterInfo, text.fontSize);

            totalLength += characterInfo.advance;
        }

        return totalLength;
    }

}

后续

这个效果基本达到要求,如果仔细看的话,并不能保证每个截取后的字符串一定是对齐的,这个也跟字符串有关,毕竟字符串长度是离散的,貌似没有办法像word一样在一行多一个文字的时候还可以挤一挤放下~

VR开发或者unity相关交流可以邮件madcloudsong@qq .com
转载请注明原文链接
http://blog.csdn.net/madcloud...

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
梦
4年前
微信小程序new Date()转换时间异常问题
微信小程序苹果手机页面上显示时间异常,安卓机正常问题image(https://imghelloworld.osscnbeijing.aliyuncs.com/imgs/b691e1230e2f15efbd81fe11ef734d4f.png)错误代码vardate'2021030617:00:00'vardateT
Peter20 Peter20
4年前
mysql中like用法
like的通配符有两种%(百分号):代表零个、一个或者多个字符。\(下划线):代表一个数字或者字符。1\.name以"李"开头wherenamelike'李%'2\.name中包含"云",“云”可以在任何位置wherenamelike'%云%'3\.第二个和第三个字符是0的值wheresalarylike'\00%'4\
Wesley13 Wesley13
3年前
Java爬虫之JSoup使用教程
title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
Unity横屏
Android下发现Unity里面的Player设置,并不能完全有效,比如打开了自动旋转,启动的时候还是会横屏,修改XML添加以下代码<applicationandroid:icon"@drawable/ic\_launcher"                    android:label"@string/app\_name"
Wesley13 Wesley13
3年前
unity将 -u4E00 这种 编码 转汉字 方法
 unity中直接使用 JsonMapper.ToJson(对象),取到的字符串,里面汉字可能是\\u4E00类似这种其实也不用转,服务器会通过类似fastjson发序列化的方式,将json转对象,获取对象的值就是中文但是有时服务器要求将传参中字符串中类似\\u4E00这种转汉字,就需要下面 publ
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
4个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
鬼子母
鬼子母
Lv1
已知泉路近,欲别故乡难。
文章
5
粉丝
0
获赞
0