- Home /
How to resize an InputField according to the content which is dynamic?
I've got an InputField where the text is filled in upon getting a string back from the server. We can debug the entire text so we receive it correctly. However the text is too long to fit in our InputField. What I basicly need is the idea of the content size fitter provided by unity (Content size fitter isn't supported for InputFields by design) At the moment we are trying to update the size of the InputField with the following snippet of code.
var x = new GUIStyle();
var rectTransform = pvText.GetComponent<RectTransform>();
var words = pvText.text.Split(' ');
var currentWidth = 0f;
var currentHeight = 0f;
foreach (var word in words)
{
var size = x.CalcSize(new GUIContent(word + "A"));
var height = x.CalcHeight(new GUIContent(word), size.x);
currentWidth += size.x;
if (currentWidth >= rectTransform.sizeDelta.x)
{
currentWidth = size.x;
currentHeight += height + pvText.textComponent.lineSpacing;
}
}
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, currentHeight);
Comment
Your answer
