Unity 中給定寬度截斷text內容
阿新 • • 發佈:2017-10-09
截斷 lte mat ++ out substring all isn ring
在Unity開發中經常會遇到這樣的問題,如果text的內容超過給定的寬度則截斷並追加 “...”。
以下便是解決該問題的代碼
/// <summary> /// 根據給定的寬度截斷字符串。並將給定的後綴拼接到截斷後的字符串。 /// </summary> /// <param name="text"></param> /// <param name="maxWidth"></param> /// <param name="suffix"></param> /// <returns></returns>public static string StringEllipsis(Text text,int maxWidth,string suffix = "...") { int textLeng = GetTextLeng(text); if(textLeng > maxWidth) { int suffixLeng = GetTextLeng(text, suffix); return StripLength(text, maxWidth - suffixLeng) + suffix; }else { return text.text; } } /// <summary> /// 按照指定寬度截斷字符串 /// </summary> /// <param name="text"></param> /// <param name="width"></param> /// <returns></returns> public 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> /// 獲取字符串在text中的長度 /// </summary> /// <param name="text"></param> /// <param name="str"></param> /// <returns></returns> public static int GetTextLeng(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; }
當然這類方法也可以寫成Text的擴展,方便調用
public static class ExtendText { public static void StringEllipsis(this Text text,int maxWidth,string suffix = "...") { int textLeng = GetTextLeng(text); if (textLeng > maxWidth) { int suffixLeng = GetTextLeng(text, suffix); text.text = StripLength(text, maxWidth - suffixLeng) + suffix; } 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> /// 獲取字符串在text中的長度 /// </summary> /// <param name="text"></param> /// <param name="str"></param> /// <returns></returns> private static int GetTextLeng(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; } }
Unity 中給定寬度截斷text內容