前言
在unity的ugui中Text控件,有时我们会有各种各样的需求,比如类似html中css的text-overflow属性,希望一段文字如果不够长就全显示,如果特别长就截断并且后面加上例如…这种后缀。
好吧这样的需求在ugui里貌似没有现成的方法,如果有的话麻烦指点一下~
实现
大概的思路就是
- 首先要能判断什么时候overflow
- 并且支持加上后缀
那么text控件本来是支持overflow然后直接截断的,但是比较暴力,直接砍断,不能加后缀,并不满足我们的需求。
然后如果简单的通过字符个数截断,那根本不行,如果根据中英文字符来根据长度截断,这个我试过,然而字体并不一定是一个中文相当于俩英文字符,于是乎如果有一大排lllll或者iii什么的,悲剧无以言表。
所以我们需要知道一段文字所对应的渲染之后的长度。如果从text的preferwidth或者通过添加content size filter组件应该也能完成类似任务,不过我倾向于直接算好长度去填充。
这个功能核心代码为
Font myFont = text.font
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" };
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);
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);
}
string StripLength(string input, int maxWidth = MAX_WIDTH)
{
int totalLength = 0;
Font myFont = text.font;
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);
}
int CalculateLengthOfText(string message)
{
int totalLength = 0;
Font myFont = text.font;
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
后续
这个效果基本达到要求,如果仔细看的话,并不能保证每个截取后的字符串一定是对齐的,这个也跟字符串有关,毕竟字符串长度是离散的,貌似没有办法像word一样在一行多一个文字的时候还可以挤一挤放下~
转载请注明原文链接
http://blog.csdn.net/madcloudsong/article/details/54670045