I’m having an issue where changing the CanvasScaler scale factor can randomly change how text is wrapped and can change the number of lines for wrapped text.
For example: In the first image, a text whose width is defined in pixels (272.4 pixels)
In this one, I change the scale factor from 1.6 to 1.5. The RectTransform for the Text component is still 272.4 units but now it’s randomly wrapped differently. And depending on different scale factors it will switch back and forth between how it wraps.
Is there a way to get text to consistently wrap when all other factors except the scale factor are the same?
Is this using UI Text or TextMeshPro?
Since UI Text uses bitmap fonts, scale changes would result in re-rasterization of the glyphs to use a different point size where the metric at that larger size would be different thus resulting in potential line width / warpping changing.
Note: In order for bitmap text to render correctly, the glyphs have to be rendered at 1:1. As such, you end up with multiple copies of the same glyph in the atlas texture (one for each point size / scale used). When the font is using hinting (usually at small point size) the shape of the glyph is altered thus resulting in varying metrics like x and y bearing and advance values. This affects layout and line width which in turn would affect word wrapping.
Thanks for the reply and the insight! I’m using UI Text.
I think I get what you’re saying. I guess no matter what the scale is, in the end, the Text rounds the size of the glyph to exactly match physical pixel sizes to maintain quality - and the rounding to do that is the wildcard? I also see what you’re talking about with generating multiple altas versions; noted, I’ve been warned.
I also tested TextMeshPro and saw it scales better in that it doesn’t have the wrapping issues. But, it’s invasive to swap it in this far along in the project, and I like the look of UI Text better.
I found a workaround hack, it’s not clean, but it’s only used in a few rare places and does the job. I subclassed from UnityEngine.UI.Text and modified OnPopulateMesh() to evaluate what the generated text looks like at 1.0 scale - then when I generate the actual geometry, I twiddle with the x extent and iterate if the number of lines doesn’t match my unscaled reference.
1 Like