Author Topic: Automatically resizing labels?  (Read 14776 times)

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Automatically resizing labels?
« on: September 06, 2012, 08:16:25 AM »
Is there a way to make labels automatically resize so that they always fit within a certain region? This would be especially useful for localizing text in labels and for any labels that have dynamic text really.
« Last Edit: September 06, 2012, 08:47:16 AM by Fordeka »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Automatically resizing labels?
« Reply #1 on: September 06, 2012, 02:18:26 PM »
If you set number of lines to 1 and set the line width to something other than 0 (for example 100 pixels), your label will never exceed this requirement (1 line in height and 100 pixels in width).

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Automatically resizing labels?
« Reply #2 on: September 06, 2012, 02:23:55 PM »
Wouldn't it just cut off any characters that don't fit in that case?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Automatically resizing labels?
« Reply #3 on: September 06, 2012, 02:24:24 PM »
Yup. Do you want it to shrink the label until it fits?

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Automatically resizing labels?
« Reply #4 on: September 06, 2012, 07:42:26 PM »
I believe so, but perhaps you know a better way- in the cases I'm dealing with the text going into the UILabel is either localized or dynamic for some other reason (a player's name for instance) and it has to fit within a certain width as to not break the design of the user interface (overflow and such). It would also look wierd if the number of characters is significantly less than the norm so you would probably want it to scale up too. I think if it just scaled up or down so that the width of the label (or text on the label) was equal to the specified maximum allowed width of the element (that you would specify in the inspector) that would fix the problem, or can you suggest an alternative way to deal with this?
« Last Edit: September 06, 2012, 07:46:42 PM by Fordeka »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Automatically resizing labels?
« Reply #5 on: September 07, 2012, 06:47:56 AM »
I would suggest using a non-limited label, and put a script on it that scales it based on UILabel.relativeSize of the text (it's a relative size, so 1.0 means 100% of font's height).

Fordeka

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Automatically resizing labels?
« Reply #6 on: September 07, 2012, 08:55:39 AM »
Thanks! That worked perfectly.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Automatically resizing labels?
« Reply #7 on: September 08, 2012, 04:10:03 PM »
Maybe you should have something like that by default in NGUI ? if there is a lineWidth != 0 AND we pressed a boolean then the label should stretch.

seandanger

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 32
    • View Profile
    • Bit By Bit Studios
Re: Automatically resizing labels?
« Reply #8 on: September 26, 2012, 06:33:38 PM »
Maybe you should have something like that by default in NGUI ? if there is a lineWidth != 0 AND we pressed a boolean then the label should stretch.

I second that :)

@ArenMook

What's the proper way to do this if we want to have a lineWidth set (limited) on the label?  I'm using a label for speech bubbles that size to the text supplied, and also wrap to the next line once they're larger than a certain width.  For labels with lineWidth set, relativeSize.x is always based on lineWidth, even if the content is shorter than the lineWidth.  I think the variable I'd like to get at is the local variable "x" in UIFont::CalculatePrintedSize.  It looks like that's the size of the rendered text in pixels, which would work great for my purposes. 

I can hack it in, but I suspect there's a better way to go about it :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Automatically resizing labels?
« Reply #9 on: September 26, 2012, 08:23:33 PM »
Pseudocode:
  1. if (calculatedPrintedSizeX * fontSize > maxLineWidth)
  2. {
  3.     labelScale *= maxLineWidth / (calculatedPrintedSizeX * fontSize);
  4. }

seandanger

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 32
    • View Profile
    • Bit By Bit Studios
Re: Automatically resizing labels?
« Reply #10 on: September 26, 2012, 08:33:41 PM »
Thanks for the code, but that part I understand already.  I was wondering if there was a way to get "calculatedPrintedSizeX" already in the supplied code, but I don't see one.  I'll go ahead and add my own in.

merlin981

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Automatically resizing labels?
« Reply #11 on: December 30, 2013, 12:19:53 PM »
Prior to NGUI 3, I used UIStretch to scale my UILabels for various resolutions. This worked perfectly, and was especially handy on the cross platform game I built (consoles, PC, and mobile devices).

However, with NGUI 3, UIStretch no longer scales UILabels. It actually seems to do nothing to the label. I've tried using extreme values just as a test, and the UILabel remained the same size.

I understand I can scale the UILabel using Unity's built-in Scale property in the inspector. However, the UIStretch was perfect for auto-scaling across multiple resolutions, and when used in conjunction with UIAnchor, was the best solution I'd found for anchoring and scaling my UI.

Aron, any suggestions on how to auto-scale a UILable in v3, or how to update UIStretch to work on UILabels again?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Automatically resizing labels?
« Reply #12 on: December 30, 2013, 01:29:05 PM »
Scale is not used in NGUI 3. At all. You can still scale things yourself if you want, but the widget's region is now specified using explicit width and height values.

UIStretch has also been deprecated now that the new layout system is in place. I suggest you watch the layout system videos that were released with 3.0.7. They're linked in the NGUI's documentation thread. The new layout system is far more powerful than the UIAnchor+UIStretch combo.