- Home /
how to normalize font size across multiple UI text field using "best fit" and "max size"
Hi, I'm using "best fit" to display text in my UI buttons.
I first tried to set Max Size to something that looked good on desktop, but when I deployed my game on iPad with its retina display, the max size was way too small. So I figured I'd use a big value for max size (was 20, set it to 80) but it results in some buttons having bigger font size than others according to the amount of text in each button.
So what's the good way to deal with this? Should I forget "best fit" and hardcode font sizes according to resolution and taking into account retina display? Or is there a way to normalize the font size across all buttons.
Answer by RowanG1 · Mar 02, 2018 at 01:41 AM
The way that has worked the best for me:
bool hasSetAllFontSizeSameSinceLastRefresh;
Update() {
if (hasSetAllFontSizeSameSinceLastRefresh == false)
{
SetAllFontSizesSame();
}
if (someConditionToUpdateTextMet) {
updateYourTextElement();
hasSetAllFontSizeSameSinceLastRefresh == false;
}
}
void SetAllFontSizesSame() {
List<int> allTextItemsFontSize = new List<>();
foreach (GameObject textItem in allYourTextGameObjectsArray) {
eachTextItemFontSize = textItem.GetComponent().cachedTextGenerator.fontSizeUsedForBestFit;
allTextItemsFontSize.Add(eachTextItemFontSize);
}
smallestFontSize = allTextItemsFontSize.Min();
float multiplier = 1 / canvas.scaleFactor;
eachTextComponent.GetComponent().resizeTextMaxSize = (int)Mathf.Floor(smallestFontSize * multiplier);
hasSetAllFontSizeSameSinceLastRefresh == true;
}
When updateYourTextElement() is run, we leave the max font size for best fit to be quite large. Or for optimal continuity, set the value a bit higher than the last smallest font size. Otherwise, you might experience "jumping" from a large font size to a small font size.
Your answer
Follow this Question
Related Questions
Is there a way to find the current font size of a Text? 2 Answers
When game resolution is 4k, UI text font size stops scaling from 145 0 Answers
How can I set the UI Text font size higher than 300px ? | Unity 5.4 2 Answers
UI Position Correction for worldspace UI? 1 Answer
Does Unity 5.6 free/pro provide support to open external applications inside the Unity scene? 0 Answers