Author Topic: AutoResizeButton ?  (Read 2782 times)

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
AutoResizeButton ?
« on: May 13, 2012, 11:22:20 PM »
Is there anyway (or an existing function I can drag in) to resize the X scale of a button depending on the width of the text?

Right now unless I make a particular button wide enough for 2 different text strings, text in 1 language will overflow out of the button, but doing so sometimes mean the other text string is too small in a wide button.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: AutoResizeButton ?
« Reply #1 on: May 14, 2012, 12:08:23 AM »
Automatically? No, you'll need to account for it manually, or just set the maximum length of your label so it doesn't overflow.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: AutoResizeButton ?
« Reply #2 on: May 14, 2012, 12:26:36 AM »
Hmm, not automatic huh. Well I suppose it was to be expected. It is going to be kind of tricky though to automatically have a Localization and except things to interchange easily with big buttons.

Well, will have to see how it pans out. I guess if I am desperate there is no reason why I can't put a hack in there to automically make a button bigger when using a certain text string that is called from within the code.

dlewis

  • Guest
Re: AutoResizeButton ?
« Reply #3 on: May 14, 2012, 01:11:20 AM »
Well, will have to see how it pans out. I guess if I am desperate there is no reason why I can't put a hack in there to automically make a button bigger when using a certain text string that is called from within the code.

I made a function that took the length of a string and put arrows on the edge of either side of the string. I'll post the code in case you or anyone else is interested. It was decided that the feature was not desired so I stopped working on it (mostly done)

  1. public UILabel data; // The label displaying the data (ie. Night)
  2.  
  3. public UISprite arrow_left; // Arrow indicating there are more items to the left (also clickable on PC)
  4. public UISprite arrow_right; // Arrow indicating there are more items to the right (also clickable on PC)
  5.  
  6. //public float arrowOffset = 0.0f; // How far off the label (left and right) should the arrow be?
  7.  
  8. // Update the position of the arrows so that they are in the right spot depending on the text length
  9. public virtual void UpdateArrowPosition()
  10. {
  11.         float dataMidPos = data.cachedTransform.localPosition.x;
  12.         float dataWidth = data.relativeSize.x * data.cachedTransform.localScale.x; // How long it is
  13.  
  14.         Vector3 leftArrowPos = arrow_left.cachedTransform.localPosition;
  15.         Vector3 rightArrowPos = arrow_right.cachedTransform.localPosition;
  16.  
  17.         // Offset the position
  18.         //leftArrowPos.x -= arrowOffset;
  19.         //rightArrowPos.x += arrowOffset;
  20.  
  21.         // Arrow must be midPos +- (dataWidth / 2)
  22.         leftArrowPos.x = dataMidPos - (dataWidth / 2);
  23.         rightArrowPos.x = dataMidPos + (dataWidth / 2);
  24.  
  25.         // Set the values
  26.         arrow_left.transform.localPosition = leftArrowPos;
  27.         arrow_right.transform.localPosition = rightArrowPos;
  28. }
  29.  

All you would need to do for your case would be to get the length of the string in english, size in the other language and then get the percentage of how much bigger it is and then multiply the width/scale of the button by that value (dataWith is the length of the string in my code if it's not clear).
« Last Edit: May 14, 2012, 01:27:02 AM by dlewis »

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: AutoResizeButton ?
« Reply #4 on: May 14, 2012, 09:07:59 PM »
Cheers for that Dlweis, that is actually the sort of thing I was planning on doing myself. The only thing I am concerned about maybe is getting it to work correctly with the buttons being pushed etc. Well, it is something to look into later anyway.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: AutoResizeButton ?
« Reply #5 on: May 16, 2012, 01:26:35 AM »
Ok, so I have my code to search through all the buttons in the menu and adjust the size accordingly.

The problem I am having is that I am searching through all my labels using

foreach ( UILabel label in text_strings)

First problem with that is that I am also finding labels that are used for non button strings.

Next problem is the same when searching for the images to resize using

foreach ( UISlicedSprite button in button_bg)



I guess what I am looking for is to access both lists, something like this:

foreach UIButton button in button_list
{
    Debug.Log(button.label.cachedTransform.localPosition.x);
    Debug.Log(button.slicedsprite.relativeSize.x);
}

But as far as I can tell UIButton is not actually anything special in itself and is simply a Unity Object with a sprite and text string attached so you can't reference them like that.