Author Topic: Fit label in a box  (Read 5768 times)

MasterKelli

  • Guest
Fit label in a box
« on: August 30, 2012, 09:39:14 AM »
Hi,

I didn't find any included script that would scale a label to predefined box area.
I think it is a very important feature when considering localizations and custom input.
ie. If you want the text to fit inside a button even if the text might be very different length in different languages. 

Luckily the NGUI is flexible enough that making such script was simple enough.
In case someone else needs this I'll post the code here.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [ExecuteInEditMode]
  5. [RequireComponent(typeof(UIBaseLabel))]
  6. public class LabelFit : MonoBehaviour
  7. {
  8.     public enum FitType
  9.     {
  10.         First,
  11.         Horizontal,
  12.         Vertical,
  13.         Both
  14.     }
  15.  
  16.     public FitType type = FitType.First;
  17.     public float width = 100;
  18.     public float height = 100;
  19.     UIBaseLabel m_label = null;
  20.  
  21.         void OnEnable()
  22.     {
  23.         m_label = GetComponent<UIBaseLabel>();
  24.         }
  25.        
  26.         public void Update()
  27.     {
  28.         float width_factor = width / m_label.relativeSize.x;
  29.         float height_factor = height / m_label.relativeSize.y;
  30.  
  31.         Vector3 local_scale = m_label.transform.localScale;
  32.  
  33.         if (type == FitType.First)
  34.         {
  35.             if (width_factor < height_factor) // Find first limiting factor and adjust the other one to achieve uniform scaling
  36.                 local_scale.x = local_scale.y = width_factor;
  37.             else
  38.                 local_scale.x = local_scale.y = height_factor;
  39.         }
  40.         else
  41.         {
  42.             if (type == FitType.Both || type == FitType.Horizontal)
  43.                 local_scale.x = width_factor;
  44.             if (type == FitType.Both || type == FitType.Vertical)
  45.                 local_scale.y = height_factor;
  46.         }
  47.  
  48.         m_label.transform.localScale = local_scale;
  49.         }
  50.  
  51.     public void OnDrawGizmos()
  52.     {
  53.         // Handles only the case when pivot is in center.
  54.         Gizmos.color = Color.green;
  55.         Gizmos.DrawWireCube(m_label.transform.position, new Vector3(width * transform.parent.lossyScale.x, height * transform.parent.lossyScale.y, 0.01f));
  56.     }
  57. }
  58.  

Usage:

Attach this script to a label, set the fit type and adjust the box size. Doesn't store the original scale before attaching the label, that could be added to make it more convenient to switch between fit types.

PhilipC

  • Guest
Re: Fit label in a box
« Reply #1 on: August 30, 2012, 11:46:16 AM »
Thanks!

Maybe I'm miss reading the code but is this not similar to UIStretch with a specified container?

MasterKelli

  • Guest
Re: Fit label in a box
« Reply #2 on: August 31, 2012, 01:00:03 AM »
Maybe I'm miss reading the code but is this not similar to UIStretch with a specified container?

Could be the same. How do you then specify a container for UIStretch? I can't find any way at least in the free version.
We are planning on buying the package as soon we are sure it supports all the functionality we need in our menus.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fit label in a box
« Reply #3 on: August 31, 2012, 02:13:59 PM »
Free version doesn't have it. It was added recently, and Free version is very old.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Fit label in a box
« Reply #4 on: September 03, 2012, 04:20:24 AM »
Following the thread here, how do you set up UIStretch to make a label resize and fit in a specific size ?
I've made this :
- ParentLabel (UIStretch)
-- Label (UILabel)

but the result I have is a label with a huge width...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fit label in a box
« Reply #5 on: September 03, 2012, 07:21:24 AM »
UIStretch doesn't do anything with labels right now. The script you'd want for this would modify the UILabel.lineWidth rather than scale, and UIStretch modifies the scale.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Fit label in a box
« Reply #6 on: September 03, 2012, 08:57:22 AM »
I don't really get it, I have a UILabel with lineWidth at 300 for example, and when I attach a UIStretch to this UILabel, it just take the all screen width...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fit label in a box
« Reply #7 on: September 03, 2012, 09:55:48 AM »
As I said, UIStretch is not coded to stretch labels. Label "scale" is the size of the font. So if the font is size 24, label's scale will be (24, 24, 1), regardless of whether it's 2 characters or 1000 long.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Fit label in a box
« Reply #8 on: September 03, 2012, 10:18:23 AM »
Okay lol. I though it was possible because Philip said it was similar to UIStretch.