Author Topic: UIFont.CalculatePrintedSize alternatives  (Read 5716 times)

Kolyunya

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
UIFont.CalculatePrintedSize alternatives
« on: January 23, 2014, 04:53:32 AM »
One of the latest NGUI updates broke my code which used UIFont.CalculatePrintedSize method.
What are the alternatives which are available in the latest release?
Why was the method removed?
Thank you for help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #1 on: January 23, 2014, 05:20:32 AM »
It wasn't removed, it was moved to NGUIText.

Kolyunya

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #2 on: January 23, 2014, 06:16:07 AM »
ArenMook, why doesn't this function depend on the particular font? Different fonts should produce different values, should not they?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #3 on: January 23, 2014, 06:20:31 AM »
Because you should call UILabel's UpdateNGUIText() prior to using NGUIText functions. It's what sets the font and everything else.

Zoidster

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #4 on: December 15, 2014, 06:59:55 AM »
Digging out this old topic.
Can you tell me how I can calculate the width of a string in a label of a given font and fontsize (without writing my own CalculatePrintedSize method)?
I want to calculate the width to see if I have to add a linebreak.
« Last Edit: December 15, 2014, 07:08:50 AM by Zoidster »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #5 on: December 15, 2014, 12:51:30 PM »
Just use the existing CalculatePrintedSize. Why write your own? I don't quite understand the question. UpdateNGUIText() function sets everything NGUIText needs to work. If you want a different font size, just set it prior to calling CalculatePrintedSize -- NGUIText.fontSize = 32;

Zoidster

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #6 on: December 15, 2014, 12:54:28 PM »
I'm going to try that first thing tomorrow. Thanks.

Zoidster

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #7 on: December 16, 2014, 06:16:48 AM »
Ok, that works if I also adjust the rect size in nguitext. My problem was that I put the text in, then updated nguitext, and then calculated the size. The vector2 I got out of the calculation was not the size of the text as one row, but the size of the label.
« Last Edit: December 16, 2014, 06:22:35 AM by Zoidster »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #8 on: December 17, 2014, 03:02:16 PM »
Try posting the code you're using, it might help me understand what you're doing wrong.

Zoidster

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIFont.CalculatePrintedSize alternatives
« Reply #9 on: December 18, 2014, 02:40:14 AM »
Its working now, as I said.
This was my code before:
  1. label.text = myText;
  2. label.UpdateNGUIText();

The problem was that I wanted the width of "Example text" and the calc method gave me the width (and height) of
"Example
text"

That was, because my label wasn't wide enough for the text. Now that I added these lines
NGUIText.rectWidth = int.MaxValue;
NGUIText.rectHeight = int.MaxValue;

The method calculates the width of the text as one line rather than breaking it up into multiple lines.