Author Topic: How do I resize Labels that are instantiated run-time?  (Read 4722 times)

dwjkim

  • Guest
How do I resize Labels that are instantiated run-time?
« on: August 30, 2013, 01:42:31 AM »
I coded to resize the label size but obviously the font size is read-only.

               subject[idx] = NGUITools.AddChild(mPanel, mLabelPrefab);      
               label = subject[idx].GetComponent<UILabel>();
               label.font.size = 14;

When the labels are instantiated, they are automatically set at 1, 1, 1.

What can I do?

dkozlovtsev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: How do I resize Labels that are instantiated run-time?
« Reply #1 on: August 30, 2013, 01:47:09 AM »
i can be wrong, but UILabel is derived from UIWindget which is derivative of MonoBehavior, so it is safe to assume that you can use label.transform

dwjkim

  • Guest
Re: How do I resize Labels that are instantiated run-time?
« Reply #2 on: August 30, 2013, 02:49:42 AM »
               subject[idx] = NGUITools.AddChild(mPanel, mLabelPrefab);      
               label = subject[idx].GetComponent<UILabel>();
            Vector3 scale = new Vector3(14f, 14f, 14f);
               label.transform.localScale.Scale(scale);

Tried this code and nothing changes. Any other ideas?

dkozlovtsev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: How do I resize Labels that are instantiated run-time?
« Reply #3 on: August 30, 2013, 03:25:27 AM »
Weird, it works for me. try calling MarkAsChanged() on it

dwjkim

  • Guest
Re: How do I resize Labels that are instantiated run-time?
« Reply #4 on: August 30, 2013, 04:51:57 AM »
               label = subject[idx].GetComponentInChildren<UILabel>();
               subject[idx].AddComponent<UIButtonMessage>();
               msg = subject[idx].GetComponent<UIButtonMessage>();
               msg.target = gameObject;
               
               msg.functionName = "ClickSubject";
               msg.trigger = UIButtonMessage.Trigger.OnClick;
            label.text = scn;         
            subject[idx].AddComponent<Task>();
            Task task = subject[idx].GetComponent<Task>();
            task.tasklevel = level;
            task.taskid = idx;
            Vector3 scale = new Vector3(2f, 2f, 0);
            subject[idx].GetComponent<Transform>().localScale.Scale (scale);


I have no idea what that MarkedAsChanged is.

I tried this code and it still does not work. I tried using button instead of label and it does not get resized.

Would it be because the panel is a UI Table? I think I tried around that but I do not think it is because the panel is UI Table.

buttons are clones of a single prefab.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How do I resize Labels that are instantiated run-time?
« Reply #5 on: August 30, 2013, 05:01:16 PM »
label.transform.localScale = new Vector3(300f, 200f, 1f);