ALFA.Shared.DisplayString.ShortenStringToWidth C# (CSharp) Method

ShortenStringToWidth() public static method

public static ShortenStringToWidth ( string input, int pixels ) : string
input string
pixels int
return string
        public static string ShortenStringToWidth(string input, int pixels)
        {
            if (pfc.Families.Length == 0)
            {
                lock (pfc)
                {
                    if (pfc.Families.Length == 0)
                        pfc.AddFontFile(ALFA.SystemInfo.GetGameInstallationDirectory() + @"\UI\default\fonts\NWN2_Main.ttf");
                }
            }
            
            float width = g.MeasureString(input, new Font(pfc.Families[0], 10.0f)).Width;
            bool shortened = false;
            while (width > pixels)
            {
                input = input.Substring(0, input.Length - 1);
                width = g.MeasureString(input, new Font(pfc.Families[0], 10.0f)).Width;
                shortened = true;
            }
            input.Trim();
            if (shortened) input += "...";

            return input;
        }
    }

Usage Example

Ejemplo n.º 1
0
 public void ConfigureDisplayName()
 {
     DisplayName = "  " + this.LocalizedName;
     DisplayName = DisplayString.ShortenStringToWidth(DisplayName, 250);
 }
All Usage Examples Of ALFA.Shared.DisplayString::ShortenStringToWidth