Hello Guest

Author Topic: Programtically changing button text  (Read 4314 times)

markmd76

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Programtically changing button text
« on: August 17, 2013, 08:59:57 am »
I am changing my button text at runtime using:
Code: [Select]
tk2dTextMesh textMesh = child.GetComponentInChildren<tk2dTextMesh>();
textMesh.text = "My New Text";
textMesh.Commit();

However this text does not seem to fit.  It is as if the "Fit" button needs clicking in the inspector.  How doe I do this at runtime?

Thanks.

markmd76

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Programtically changing button text
« Reply #1 on: August 17, 2013, 09:32:48 am »
Bloody typical that!  I have found the solution.

I needed to add the following:
Code: [Select]
textMesh.maxChars = textMesh.text.Length;
The only thing is that there is a  note saying that it will "free & allocate memory, avoid using at runtime.".  Is there a better way to achieve this?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Programtically changing button text
« Reply #2 on: August 17, 2013, 12:35:49 pm »
You should set maxchars in the inspector to what you'd think is a sensible maximum in the inspector. This allows you to avoid memory allocation at runtime. If you don't care about that though, then you can simply set textMesh.maxChars to your max string length.

The code tries hard to avoid memory allocations, and with a little care you can avoid runtime allocations altogether.