public static class TextExtension
{
/// <summary>
/// 设置文本长度
/// </summary>
/// <param name="text"></param>
/// <param name="maxWidth"></param>
/// <param name="input">输入框</param>
/// <param name="suffix"></param>
public static void SetTextWithEllipsis(this Text text, int maxWidth, InputField input = null, string suffix = "...")
{
int textLeng = GetTextLeng(text);
if (textLeng > maxWidth)
{
int suffixLeng = GetTextLeng(text, suffix);
if(input == null)
{
text.text = StripLength(text, maxWidth - suffixLeng) + suffix;
}
else
{
input.text = StripLength(text, maxWidth - suffixLeng) + suffix;
}
}
else
{
return;
}
}
/// <summary>
/// 中间添加省略号
/// </summary>
/// <param name="text"></param>
/// <param name="maxWidth"></param>
/// <param name="suffix"></param>
public static void SetTextWithMiddle(this Text text, int maxWidth, string suffix = "...")
{
int textLeng = GetTextLeng(text);
if (textLeng > maxWidth)
{
int suffixLeng = GetTextLeng(text, suffix);
int middle = Mathf.Abs((maxWidth - suffixLeng)/2);
string startStr = StripLength(text, middle);
string endStr = StripEndLength(text, middle);
text.text = startStr + suffix + endStr;
}
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>
/// 按照指定宽度截断字符串
/// </summary>
/// <param name="text"></param>
/// <param name="width"></param>
/// <returns></returns>
private static string StripEndLength(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 -1; i >= 0; 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.Remove(0,i);
}
/// <summary>
/// 获取字符串在text中的长度
/// </summary>
/// <param name="text"></param>
/// <param name="str"></param>
/// <returns></returns>
public static int GetTextLeng(this 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;
}
/// <summary>
/// 文本长度是否超出
/// </summary>
/// <param name="text"></param>
/// <param name="str"></param>
/// <returns></returns>
public static bool OutLength(this Text text, int maxWidth)
{
int textLeng = GetTextLeng(text);
if (textLeng > maxWidth)
{
return true;
}
return false;
}
}
Untiy:Text文本超长的时候省略展示
最新推荐文章于 2024-05-17 18:46:40 发布