- Home /
TextGenerator GetPreferredHeight calculating wrong height
When using the cachedTextGenerator from a Text field to calculate the necessary height for a certain text, I get a height value for 2 lines of text, even though the final Text UI on screen fits the text in one line.
I checked the with of the text field in the editor and it is exactly the same as used for the generator.
Am I doing something wrong besides doing the following steps? - get the cachedGenerator - get the Settings from the Text UI with GetGenerationSettings - calling GetPreferredHeight on the generator with the with I just mentioned and with the grabbed settings
It might be worth mentioning that I generate the settings with a Vector2 with the correct width, but with 0 height, since I don't need it.
Any ideas?
Did you ever find a solution to this? I'm having the opposite problem - I also get the height for 2 lines of text but I have 4.
Never $$anonymous$$d,
float h = LayoutUtility.GetPreferredHeight( _text.rectTransform );
Seems to do the trick in less code :)
I've recently encountered this with
float preferredHeight = textGenerator.GetPreferredHeight(content.text, generationSettings);
This would produce something like 3000 preferredHeight when in reality it was ~150. The solution was to remove a content size fitter component that I had on that content game object. This might be a bug.
Answer by wightwhale · Oct 20, 2017 at 07:31 PM
There's a bug filed on this issue. I'm running into a similar problem as well.
Your answer
Follow this Question
Related Questions
Text Preferred Height not expanding even with content size fitter 0 Answers
Resize scrollview height to match size of box inside 0 Answers
height and width of label/box depending on amount of text 2 Answers
[UI Text] Half the font size but still keeping the same line height. 0 Answers
Generating Text with a script - setting width/height? 0 Answers