unity里获取text中文字宽度并截断省略的操作
作者:狂云歌 发布时间:2023-11-02 07:48:52
前言
在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就拿到了每个字符的渲染长度。
拿到每个字符长度之后那就简单多了,计算一下需要截断的字符总长度,如果大于限制长度,就除去后缀长度后,截取子字符串,然后再接上后缀。这个事情就搞定了。
全部如下,这个例子是需要一个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一样在一行多一个文字的时候还可以挤一挤放下~
补充:Unity Text文字超框,末尾显示‘...'省略号
// 超框显示...
public static void SetTextWithEllipsis(this Text textComponent, string value)
{
var generator = new TextGenerator();
var rectTransform = textComponent.GetComponent<RectTransform>();
var settings = textComponent.GetGenerationSettings(rectTransform.rect.size);
generator.Populate(value, settings);
var characterCountVisible = generator.characterCountVisible;
var updatedText = value;
if (value.Length > characterCountVisible)
{
updatedText = value.Substring(0, characterCountVisible - 3);
updatedText += "…";
}
textComponent.text = updatedText;
}
调用方式:在给Text赋值的时候调用一次即可
text.SetTextWithEllipsis(titleDesc);
效果如下:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/madcloudsong/article/details/54670045


猜你喜欢
- 初次使用IDEA,创建一个maven工程,发现在目录结构中产生了两个不一样的东西——.iml文件和.idea文件夹。非常好奇,所以立刻上网查
- 1:@Qualifier@Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了。所以@Auto
- 在进行详解之前,我想先声明一下,本次我们进行讲解说明的是 Kafka 消息存储的信息文件内容,不是所谓的 Kafka 服务器运行产生的日志文
- 本文实例讲述了Android编程使WebView支持HTML5 Video全屏播放的解决方法。分享给大家供大家参考,具体如下:1)需要在An
- Spring Cloud Gateway使用Spring Cloud Gateway是一个基于Spring Boot 2.x和Spring&
- 定义JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动
- 方式1:1. 明确 Spark中Job 与 Streaming中 Job 的区别1.1 Spark Core一个 RDD DAG Graph
- 前言在之前的Spring Boot基础教程系列中,已经通过《Spring Boot中使用@Async实现异步调用》一文介绍过如何使用@Asy
- 之前在博文中为了更好的给大家演示APP的实现效果,本人了解学习了几种给手机录屏的方法,今天就给大家介绍两种我个人用的比较舒服的两种方法:(1
- 该方法针对idea版本(2020.2.x)C:\Users\yanghao\AppData\Roaming\JetBrains\Intell
- 这篇文章主要介绍了如何使用java修改文件所有者及其权限,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- Java Hutool 包工具类推荐 ExcelUtil包引入hutool包版本号可根据实际情况更换<dependency> &
- 协议做如下规定:规定数据协议:序列号 长度 状态字 数据长度 数据1 &n
- 译文链接:https://www.infoworld.com/art...C# 在 3.0 版本中提供了对 扩展方法 的支持,扩展方法常用于
- 给对象中的包装类设置默认值处理方法如下主要适用于,对象中使用了包装类,但是不能给null需要有默认值的情况/**
- 关于实现网易新闻客户端的界面,以前写过很多博客,请参考:Android实现网易新闻客户端效果Android实现网易新闻客户端侧滑菜单(一)A
- 在ios7中,苹果的原生态应用几乎都能够通过向右滑动来返回到前一个页面,这样可以避免用户在单手操作时用大拇指去点击那个遥远的返回键(ipho
- Dictionary<TKey,TValue> 类,表示键和值的集合。Dictionary<TKey,TValue>
- Kotlin 基础教程之异常概述在Kotlin-null的处理里提到的NPE,它就是一个异常。而,异常是程序运行过程中出现的错误。在Kotl
- 研究了下这个,记录下代码。主页面代码:activity_main.xml<?xml version="1.0" e