- Home /
[UI] "Best fit" in text object stretches a single word across two lines
Here are the settings of my text object and how it looks like in the editor:
I'm using a custom font and have "Best fit" enabled with a big enough min-max to allow for essentially any amount of resizing. However, when I enter play mode, this happens:
As you can see, the word was cut in two parts and rendered in different lines.
Q: How can I prevent Unity from doing this without turning off "Best fit"?
You don't have to post a full answer. A link is enough.
Answer by DiegoSLTS · Jul 18, 2015 at 10:45 PM
I think there's no simple way to tell Unity not to do that, but there should be a checkbox when turning on "Best fit" with wrap and truncate to tell it to not cut words.
Anyway, I guess you can fix that with a script. I haven't tried this, but I guess it should work as long as you want a single line.
Get the font size selected by Unity to do the best fit.
Get the height of the text component.
If the height is at least twice the font size, shrink the component to the fontSize value.
That should force Unity to shrink the font again.
I did a quick script that seems to work, but I haven't tested it a lot. You have to use an anchoring setting in your text that allows you to change the height of the component (so sizeDelta.y is the height).
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BestFitWithoutCutting : MonoBehaviour {
public Text text;
void Start () {
StartCoroutine (FixHeight ());
}
IEnumerator FixHeight() {
yield return null;
TextGenerator gen = text.cachedTextGenerator;
Rect rect = text.rectTransform.rect;
while (rect.height >= 2*gen.fontSizeUsedForBestFit) {
text.rectTransform.sizeDelta = new Vector2(text.rectTransform.sizeDelta.x,gen.fontSizeUsedForBestFit);
yield return null;
rect = text.rectTransform.rect;
}
}
}
Thank you Diego, you script worked perfectly in the Editor but when exported the game on android it suddenly stopped working, I fixed liked that(this works on both editor and android):
IEnumerator FixHeight()
{
yield return null;
TextGenerator gen = text.cachedTextGenerator;
Rect rect = text.rectTransform.rect;
while (true)
{
while (rect.height >= 2 * gen.fontSizeUsedForBestFit)
{
text.rectTransform.sizeDelta = new Vector2(text.rectTransform.sizeDelta.x, gen.fontSizeUsedForBestFit);
yield return null;
rect = text.rectTransform.rect;
}
yield return null;
}
}
Answer by egeunlu · Jul 11, 2017 at 10:39 PM
There is actually a very simple way to force your text into a single line.
Increase the parameter "Line Spacing" to an impractical value such as 100 or 1000.
As there would not be enough space for the text to occupy more than one line with that much Line Spacing, your text would always be a single line.
Answer by Eudaimonium · Jul 18, 2015 at 05:46 PM
Best fit simply fills all available space with font, with respects to max and min size.
If you don't want it overflowing into two rows, simply shrink the text field a bit.
I see you are using a button. Within that button there is a text field which fills it up. Make the text field not as wide, and make sure to position the anchors on the edges of the text field (not entire button!).
If your text field isn't high enough for two rows, only one row will be displayed - simple as that.
But if the font shrinks enough, there will be space for two lines. Any tampering I do with the Text Component's size will necessarily be local and not applicable to all possible resolutions.
Well, then your best bet is to use Best fit, and tweak your $$anonymous$$ax Font size so that even on the largest resolution you can support it'll still end up being only one line. That, along side of squeezing the text field height should give you something to work with.