2D Toolkit Forum

2D Toolkit => Support => Topic started by: markmd76 on August 17, 2013, 08:59:57 am

Title: Programtically changing button text
Post by: markmd76 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.
Title: Re: Programtically changing button text
Post by: markmd76 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?
Title: Re: Programtically changing button text
Post by: unikronsoftware 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.