Truncate text content for a given width in Unity

Source: Internet
Author: User
Tags abs truncated

This problem is often encountered in unity development, and if the content of text exceeds the given width, it is truncated and appended with "...".

Here's the code to fix the problem

    /// <summary>    ///truncates a string based on the given width.    And stitching the given suffix into the truncated string. /// </summary>    /// <param name= "text" ></param>    /// <param name= "MaxWidth" ></param>    /// <param name= "suffix" ></param>    /// <returns></returns>     Public Static stringStringellipsis (text text,intMaxWidth,stringsuffix ="...")    {        intTextleng =Gettextleng (text); if(Textleng >maxWidth) {            intSuffixleng =Gettextleng (text, suffix); returnStriplength (text, Maxwidth-suffixleng) +suffix; }        Else        {            returnText.text; }    }    /// <summary>    ///truncates a string by a specified width/// </summary>    /// <param name= "text" ></param>    /// <param name= "width" ></param>    /// <returns></returns>     Public Static stringStriplength (text text,intwidth) {        intTotallength =0; Font MyFont=Text.font;        Myfont.requestcharactersintexture (Text.text, Text.fontsize, Text.fontstyle); Characterinfo Characterinfo=NewCharacterinfo (); Char[] Chararr =Text.text.ToCharArray (); inti =0;  for(; I < chararr.length;i++) {myfont.getcharacterinfo (chararr[i], outCharacterinfo, text.fontsize); intNewlength = Totallength +characterinfo.advance; if(Newlength >width) {                if(Mathf.abs (Newlength-width) > Mathf.abs (Width-totallength)) {                     Break; }                Else{totallength=newlength;  Break; }} totallength+=characterinfo.advance; }        returnText.text.Substring (0, i); }    /// <summary>    ///gets the length of the string in text/// </summary>    /// <param name= "text" ></param>    /// <param name= "str" ></param>    /// <returns></returns>     Public Static intGettextleng (text text,stringStr=NULL) {Font Mfont=Text.font; stringMSTR =string. IsNullOrEmpty (str)?Text.text:str;        Mfont.requestcharactersintexture (MSTR, Text.fontsize, Text.fontstyle); Char[] Chararr =Mstr.tochararray (); intTotaltextleng =0; Characterinfo character=NewCharacterinfo ();  for(inti =0; i < chararr.length; i++) {mfont.getcharacterinfo (chararr[i], outcharacter, text.fontsize); Totaltextleng+=character.advance; }        returnTotaltextleng; }

Of course, this kind of method can also be written as text extension, convenient to call

 Public Static classextendtext{ Public Static voidStringellipsis ( ThisText text,intMaxWidth,stringsuffix ="...")    {        intTextleng =Gettextleng (text); if(Textleng >maxWidth) {            intSuffixleng =Gettextleng (text, suffix); Text.text= Striplength (text, Maxwidth-suffixleng) +suffix; }        Else        {            return ; }    }    /// <summary>    ///truncates a string by a specified width/// </summary>    /// <param name= "text" ></param>    /// <param name= "width" ></param>    /// <returns></returns>    Private Static stringStriplength (text text,intwidth) {        intTotallength =0; Font MyFont=Text.font;        Myfont.requestcharactersintexture (Text.text, Text.fontsize, Text.fontstyle); Characterinfo Characterinfo=NewCharacterinfo (); Char[] Chararr =Text.text.ToCharArray (); inti =0;  for(; i < chararr.length; i++) {myfont.getcharacterinfo (chararr[i], outCharacterinfo, text.fontsize); intNewlength = Totallength +characterinfo.advance; if(Newlength >width) {                if(Mathf.abs (Newlength-width) > Mathf.abs (Width-totallength)) {                     Break; }                Else{totallength=newlength;  Break; }} totallength+=characterinfo.advance; }        returnText.text.Substring (0, i); }    /// <summary>    ///gets the length of the string in text/// </summary>    /// <param name= "text" ></param>    /// <param name= "str" ></param>    /// <returns></returns>    Private Static intGettextleng (text text,stringstr =NULL) {Font Mfont=Text.font; stringMSTR =string. IsNullOrEmpty (str)?Text.text:str;        Mfont.requestcharactersintexture (MSTR, Text.fontsize, Text.fontstyle); Char[] Chararr =Mstr.tochararray (); intTotaltextleng =0; Characterinfo character=NewCharacterinfo ();  for(inti =0; i < chararr.length; i++) {mfont.getcharacterinfo (chararr[i], outcharacter, text.fontsize); Totaltextleng+=character.advance; }        returnTotaltextleng; }}

Truncate text content for a given width in Unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.