Author Topic: Line Width = Screen width?  (Read 1434 times)

nübi

  • Guest
Line Width = Screen width?
« on: April 04, 2013, 04:08:41 PM »
How would I get a UILabel's Line Width to equal to the screen width?

Clearly a little script but is there a shortcut?  Like auto-stretch or fill or something similar?

Thanks

nübi

  • Guest
Re: Line Width = Screen width?
« Reply #1 on: April 04, 2013, 09:44:37 PM »
OK so I assume there's no shortcut.

So I created that little script.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class LabelStretcher : MonoBehaviour {
  5.         void Start () {
  6.                 UIRoot root = NGUITools.FindInParents<UIRoot>(gameObject);
  7.                 float scale = (float)root.manualHeight / Screen.height;
  8.                
  9.                 UIAnchor anchor = GetComponent<UIAnchor>();
  10.                 float offsetX = anchor.relativeOffset.x;
  11.                 float widthPercentage = 1f - offsetX*2f;  // margin on left/right.
  12.                
  13.                 UILabel label = GetComponent<UILabel>();
  14.                 label.lineWidth = (int)(scale * Screen.width * widthPercentage);
  15.         }
  16. }
  17.