Author Topic: Calculate text width for a label  (Read 15301 times)

Sobraj

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Calculate text width for a label
« on: October 07, 2014, 08:46:03 AM »
Hi,

Is there a way to know what a length will be of a string (based on settings of a label)?

I want to know before I assign text to a label if it will fit inside the label (with a max width).
Something like:

   if(myLabel.TextWidth(myText) > myLabel.width) do something with myText;

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Calculate text width for a label
« Reply #1 on: October 07, 2014, 10:38:36 AM »
label.UpdateNGUIText();
Vector2 size = NGUIText.CalculatePrintedSize("some text");

Sobraj

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Calculate text width for a label
« Reply #2 on: October 08, 2014, 07:03:23 AM »
It does some weird things on my end.

My label has a width of 650 and has shrinkContent when I have a string which is clearly larger than the label can contain without resizing, it gives me a x value of 642.
If I put my label on clamp it does the same, and when I put it on resize freely it says it is 270.

Am I missing something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Calculate text width for a label
« Reply #3 on: October 08, 2014, 01:28:41 PM »
You need to turn off Shrink Content before doing this, otherwise it won't resize.

Sobraj

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Calculate text width for a label
« Reply #4 on: October 09, 2014, 05:56:52 AM »
When I put it on freely resize it says it is 270 wide, while it clearly is way more then 650.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Calculate text width for a label
« Reply #5 on: October 10, 2014, 02:22:54 AM »
Let me rephrase... you need to change the label to Resize Freely prior to calling NGUIText.Update.

You can also forego the whole NGUIText thing just by simply doing this:
  1. UILabel.Overflow ov = label.overflowMethod;
  2. label.overflowMethod = UILabel.Overflow.ResizeFreely;
  3. label.text = "Whatever long text you want";
  4. Debug.Log(label.printedSize);
  5. label.overflowMethod = ov;
What I typically do in Windward (for tooltips and such) is do a check -- is the printedSize.x less or greater than a specified value? if so, then I set the label's width and change the overflow method to Resize Height instead of Resize Freely.

Sobraj

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Calculate text width for a label
« Reply #6 on: October 14, 2014, 02:23:19 AM »
My label was on resize freely prior the call to UpdateNGUIText.

I used the code you supplied in your last post, works like a charm.
Thanks for your help.

cayou

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 3
  • Posts: 90
    • View Profile
Re: Calculate text width for a label
« Reply #7 on: June 17, 2015, 09:43:01 AM »
Yeah, I don't get it how you can calculate the size of the label with a static method (NGUIText.CalculatePrintedSize), how does the system know all the settings on your label? I'm VERY curious about that. It must takes into acount the current font, the font size, the spacing of the label, etc...
Calling this method for me results in very weird results too.
But still, the printedSize accessor of the UILabel makes a lot more sense.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Calculate text width for a label
« Reply #8 on: June 18, 2015, 07:48:58 AM »
UILabel.UpdateNGUIText()

cayou

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 3
  • Posts: 90
    • View Profile
Re: Calculate text width for a label
« Reply #9 on: June 18, 2015, 03:41:40 PM »
Does the combo "myLabel.UpdateNGUIText + NGUIText.CalculatePrintedSize" make the same result as "myLabel.printedSize" ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Calculate text width for a label
« Reply #10 on: June 21, 2015, 06:25:48 PM »
printedSize results in a call to ProcessText() which does more than setting NGUIText. It handles resizing of the label as well, for example.

bandingyue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Calculate text width for a label
« Reply #11 on: June 01, 2016, 09:59:53 PM »
I think maybe you should add a param to this function,a int param of lable`s size.

sometimes it is unconvinent to use label.UpdateNGUIText before.