Author Topic: Easy way to resize a button's background to match label size?  (Read 8078 times)

LordYabo

  • Guest
Hi,

I'm just starting with NGUI but I've spent HOURS googling and reading forums looking for a simple answer to this question.

I have 4 dynamic buttons.  I don't know how large (wide) the buttons need to be until run time.
I assign text to the label.
The label correctly sizes to the new text, the background does not.
How do I set the background to the new label width?

I have tried many things, including:
  1.                 GameObject p = GameObject.Find("btnAnswer4");
  2.                 UILabel l = p.GetComponentInChildren<UILabel>();
  3.                 l.text = "Thsi is a longer button";
  4.                 Vector2 size = l.font.CalculatePrintedSize(l.text, true, UIBaseFont.SymbolStyle.Colored);
  5.                 Vector2 lwCharPixSize = new Vector2(size.x * l.transform.localScale.x, size.y * l.transform.localScale.y);             
  6.                 UISlicedSprite bg = p.GetComponentInChildren<UISlicedSprite>();
  7.                 bg.transform.localScale.Set(lwCharPixSize.x, lwCharPixSize.y, bg.transform.localScale.z);
  8.                 NGUITools.AddWidgetCollider(p); //to update the collider to the new size
  9.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Easy way to resize a button's background to match label size?
« Reply #1 on: May 28, 2013, 02:44:31 PM »
You can have a look at what UITooltip does. It scales the background to envelop the text's contents.

LordYabo

  • Guest
Re: Easy way to resize a button's background to match label size?
« Reply #2 on: May 28, 2013, 02:59:39 PM »
Thanks!!!  Saved me more searching.

Here is the final code to automatically resize a button to match a dynamic label:
  1.                 GameObject go = GameObject.Find("btnAnswer4");
  2.                 UILabel l = go.GetComponentInChildren<UILabel>();
  3.                 l.text = "Thsi is a much much longer button";
  4.                 UISlicedSprite bg = go.GetComponentInChildren<UISlicedSprite>();
  5.                 Transform backgroundTrans = bg.transform;
  6.                 Vector3 mSize = l.transform.localScale;
  7.                 Transform textTrans = l.transform;
  8.                 Vector3 offset = textTrans.localPosition;
  9.                 Vector3 textScale = textTrans.localScale;
  10.  
  11.                 // Calculate the dimensions of the printed text
  12.                 mSize = l.relativeSize;
  13.  
  14.                 // Scale by the transform and adjust by the padding offset
  15.                 mSize.x *= textScale.x;
  16.                 mSize.y *= textScale.y;
  17.                 mSize.x += bg.border.x + bg.border.z + (offset.x - bg.border.x) * 2f;
  18.                 mSize.y += bg.border.y + bg.border.w + (-offset.y - bg.border.y) * 2f;
  19.                 mSize.z = 1f;
  20.  
  21.                 backgroundTrans.localScale = mSize;            
  22.                 NGUITools.AddWidgetCollider(go);
  23.  
  24.                 //bg.transform.localScale.Set(lwCharPixSize.x, lwCharPixSize.y, bg.transform.localScale.z);
  25.                 NGUITools.AddWidgetCollider(p);
  26.