- Home /
Is it possible to disable word wrapping, without enabling horizontal overflow, when BestFit is turned on?
Is it possible to disable word wrapping, without enabling horizontal overflow, when BestFit is turned on? So size of the font would be calculated based on the width of Text rather than height.
Answer by digzou · Nov 18, 2015 at 06:40 AM
Disable "Best Fit", Set Vertical OverFlow to "Truncate" and Horizontal Overflow to "Wrap". Now starting from a small size keep increasing the font size just upto the point where it does not wrap to next line.
Then set the horizontal overflow to "Overflow" [Just to make sure it does not wrap if at all by mistake.].
Then in the 'Canvas Scaler' property of the Root Canvas [add if not present], 'UI scale mode' to "Scale with screen size", add the reference resolution you are building for. Set 'Screen Match Mode' to "Match width or height" and 'Match' to "0.5"[Match height and width proportionally]. The canvas will then be able to scale your text accordingly.
Hope this helps. :)
But what if my text is dynamic? I don't know what length it will be so I cannot set font size by hand.
In that case you'll have to assume the max number of characters that your Text will hold.
Enter that text in the text box and do the above procedure.
Then enable "Best fit" , set the "$$anonymous$$ax font size" to the appropriate font size you just found doing the above procedure.
Set "Horizontal Overflow" to "Wrap".
In this way your size is never go become very large and will also resize for shorter text accordingly.
Answer by BenGrob · Aug 11, 2017 at 06:50 PM
Since it doesn't seem possible to disable word wrapping without also enabling overflow, an alternate solution is to replace all spaces in the string with non-breaking spaces. Of course, spaces are not the only characters that can be used to break a word, so this might not work in all use cases.
textComponent.text = textComponent.text.Replace (' ', '\u00A0');