在unity的界面顯示文字的時(shí)候,會(huì)出現(xiàn)字符串超出給定的范圍的情況,比如本來(lái)設(shè)定的是顯示5個(gè)字符串沈条,但是由于某種原因,字符串長(zhǎng)度變成了10诅炉,這樣顯示就會(huì)出現(xiàn)幾種異常的情況:
- 如果加了ContentSizeFilter蜡歹,并且設(shè)置了相應(yīng)的選項(xiàng)的時(shí)候,有可能會(huì)出現(xiàn)自動(dòng)換行涕烧,然后Text就會(huì)變高月而,這樣顯示就不好看;
- 如果沒(méi)有添加任何控制Text組件的控制器议纯,那么會(huì)莫名的丟掉多出來(lái)的字符串父款。
一般會(huì)將多出的字符串截掉,然后用...去替代。
首先想到的是直接根據(jù)字符串長(zhǎng)度去截取憨攒,但是效果不是很理想世杀,因?yàn)椴煌淖址秩镜膶挾炔灰粯痈渭@樣就會(huì)造成即便截取了也會(huì)達(dá)不到效果玫坛,所以后來(lái)想到根據(jù)字符串的寬度來(lái)截取替代。
參考:http://blog.csdn.net/madcloudsong/article/details/54670045
核心部分代碼:
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;
}
使用:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextController :MonoBehaviour
{
public Text text;
private string suffix = "包晰。。炕吸。";
int suffixLength = 0;
private string textToShow = "kdjfaliejlkfjlaisdjfklejfiasldkfj;ailjelkfjalskdvkljalkdjfielkjflksdfsdfa";
private string textToShow2 = "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww";
private string textToShow3 = "我咖啡機(jī)客隊(duì) 罰阿里會(huì)計(jì)法拉克京東方IE了拉進(jìn)來(lái)打開(kāi)房間啦看到邏輯拉克減肥的拉科技付款啦";
// Use this for initialization
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
}
public void ShowText()
{
text.text = StripLengthWithSuffix(textToShow3);
}
private void Init()
{
suffixLength = CalculateLengthOfText(suffix);
Debug.LogFormat("suffix length: {0}", suffixLength);
}
private string StripLengthWithSuffix(string input, int maxWidth = 460)
{
int len = CalculateLengthOfText(input);
Debug.LogFormat("input total length = {0}", len);
if(len > maxWidth)
{
return StripLength(input, maxWidth - suffixLength) + suffix;
}
else
{
return input;
}
}
private string StripLength(string input, int maxWidth = 460)
{
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("new length: {0} - total length: {1}", newLength, totalLength);
if(Mathf.Abs(newLength - maxWidth) > Mathf.Abs(maxWidth - totalLength))
{
break;
}
else
{
totalLength = newLength;
i++;
break;
}
}
totalLength += characterInfo.advance;
i++;
}
Debug.LogFormat("total length: {0}", totalLength);
return input.Substring(0, i);
}
private 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;
}
}
結(jié)果: