Author Topic: Precalculate Dimensions in UILable  (Read 2454 times)

Romulus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Precalculate Dimensions in UILable
« on: March 20, 2014, 10:11:56 AM »
We need to precalculate size of Dimensions in UILable using that code:
  1.     void CalculateSizeText(string mText)
  2.     {
  3.         const int minWidth = 300;
  4.         const int minHeight = 1;
  5.  
  6.         string mProcessedText;
  7.         NGUIText.rectWidth = 700;
  8.         NGUIText.rectHeight = 1000000;
  9.  
  10.         NGUIText.WrapText(mText, out mProcessedText, true);
  11.         int mPrintedSize = SeatOnTableBtn.GetComponentInChildren<UILabel>().fontSize;
  12.         NGUIText.fontSize = mPrintedSize;
  13.  
  14.         Vector2 mCalculatedSize = Vector2.zero;
  15.         mCalculatedSize = NGUIText.CalculatePrintedSize(mProcessedText);
  16.  
  17.         int Width = Mathf.Min(minWidth, Mathf.RoundToInt(mCalculatedSize.x));
  18.         int Height = Mathf.Max(minHeight, Mathf.RoundToInt(mCalculatedSize.y));
  19.         Debug.Log("Width=" + Width);
  20.         Debug.Log("Height=" + Height);
  21.     }



But we couldn't get the expected result: on the test string function returned those values: Width = 300, Height = 22.
If we put this string to the UILable (parameer overflow is set to ResizeHeight), we get Dimensions = 300x66.

Could you please tell me how to calculate Dimensions in UILable correctly? Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Precalculate Dimensions in UILable
« Reply #1 on: March 20, 2014, 12:58:43 PM »
label.UpdateNGUIText() must be called before any NGUIText functions.