Author Topic: How to change the size of UISprite to fit target size?  (Read 9434 times)

xiaoniaojjj

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
How to change the size of UISprite to fit target size?
« on: October 01, 2012, 04:35:20 AM »
Pls see the picture, I creat a Sprite for the background of Label, When the label size be changed,have any quick way to  resize the Sprite to fit the Label's size ?

dlewis

  • Guest
Re: How to change the size of UISprite to fit target size?
« Reply #1 on: October 01, 2012, 07:29:17 AM »
You will need to dynamically scale a sliced sprite based on the length of the string.

xiaoniaojjj

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: How to change the size of UISprite to fit target size?
« Reply #2 on: October 01, 2012, 08:05:42 AM »
Thank you man,but didn't understand you, could you give me more detail info ? I can get the Bounds of  UILabel .But i don't know how to change the size of  UISprite by the bounds.
« Last Edit: October 01, 2012, 08:08:45 AM by xiaoniaojjj »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to change the size of UISprite to fit target size?
« Reply #3 on: October 01, 2012, 04:43:45 PM »
It's just math. Knowing the size of the label (UILabel.relativeSize scaled by transform.localScale), and the size of your area, scale the label so that it fits the area.

xiaoniaojjj

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: How to change the size of UISprite to fit target size?
« Reply #4 on: October 02, 2012, 09:05:37 AM »
Thanks for reply, the below is my code, I'm not really understand the relationship between relativeSize and localScale, do you have any better way to do it?
public class TestLabel : MonoBehaviour {

   // Use this for initialization
   public UILabel uLabel;
   public UISprite uSprite;
   
   float fLabelRelativeY;
   
   void Start () {   
      NGUITools.AddWidgetCollider(uLabel.gameObject);
      fLabelRelativeY = uLabel.relativeSize.y;            
   }
   
   // Update is called once per frame
   void Update () {   
   }
   
   void OnClick(){
      uLabel.text+=" It's a lable!";      
      NGUITools.AddWidgetCollider(uLabel.gameObject);
      MDebug.Log("Label relative:"+uLabel.relativeSize);
      float fScaleY = (uLabel.relativeSize.y-fLabelRelativeY)*uLabel.transform.localScale.y;
      if(fScaleY <=0)
         fScaleY = 0;
      else{
         fLabelRelativeY = uLabel.relativeSize.y;
      }
      uSprite.transform.localScale = new Vector3(uSprite.transform.localScale.x,uSprite.transform.localScale.y+fScaleY,uSprite.transform.localScale.z);
   
   }
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to change the size of UISprite to fit target size?
« Reply #5 on: October 02, 2012, 04:18:47 PM »
"relativeScale" is the widget's scale in 'size' units: 1.0 means 100%. 2.0 means 200%. So for example if a label has a relativeScale of (30, 2), it means that it will be 30 times the localScale.x, and 2 times the localScale.y.

To get the pixel size, simply scale one by the other: Vector2.Scale(relativeScale, localScale).

Given the above example of (30, 2), if a label is using font of size 24, it means that its localScale will be (24, 24), and multiplying the two gives you (30*24, 2*24) = (720, 48) pixels.