When I change the text of a Label the layout is not immediately updated and so resolvedStyle will still contain the previous size. One could subscribe to GeometryChanged and wait for the layout to update but this complicates the code. How can I pre-calculate how big the label will become? This is not precise. Perhaps because characterinfo works in integers. Code (CSharp): CharacterInfo info; IResolvedStyle style = label.resolvedStyle; Font font = style.unityFont; float labelWidth = style.paddingLeft + style.paddingRight; for( int i = 0; i < text.Length; i++ ) { font.GetCharacterInfo( text[i], out info, (int) style.fontSize, style.unityFontStyleAndWeight ); labelWidth += info.advance; }
Hi cecarlsen, If you don't want to register to GeometryChanged event you can call "TextElement.MeasureTextSize". https://docs.unity3d.com/ScriptReference/UIElements.TextElement.MeasureTextSize.html
Sorry to resurrect this, but what if you have a container containing a label among other things and you need the width of the entire container after a label change (or after making other size-related uss changes within the container)?
In that case I would recommend to register to GeometryChanged in the container and then get resolvedStyle.width