Fit textfield size

I have a text and i want to resize the gameobject which contains the text so it hight matches to display the text.
How to do this in c#?

Add the component: “ContentSizeFilter” and change the field “Vertical Fit” to “Preferred”, this will “Drive the height based on the preferred height of the layout element.”

This is assuming that your text is using the new Unity UI system implemented in Unity 4.6. There is absolutely no script involved.

Source: Personal Experience and Redirect to... title of new-page