- Home /
How to build a bestfit font size for a group of UI Text
Hello,
I want to have the same font size for all Text element of a panel. All Text element has the bestfit option to true to be adapted to screen size.
When I do that the size of each element can be different, so I would like to set the font size of each element to the min font size of the panel.
Kind of function all text font size = Min ( all element best fit font size ).
Thanks Best regards
Sorry but i don't get it, if you want all text elements to be the same size just set all fonts to 16 or something, if you don't want your fonts getting bigger than 16 set 16 as maximum size and the $$anonymous$$imum size to anything that will fit the panel. Using best fit he will always try to put the bigger size possible until the max size.
I'm ok to have the bigest size possible, but I'm want to have the size in all text of a panel. if I have 2 text with the same size but 2 different number of character, one font will be bigger than the other. I want that the font is adapted with the screen size but that the 2 text have the save size.
Answer by Gwom · Apr 01, 2017 at 06:57 AM
I had a similar problem, i put this in the parent and assign all Text items i want to the array in the editor. Cant get it to work in the editor though due to the way the font is made:
using UnityEngine.UI;
public class ConsistantTextSize : MonoBehaviour {
public Text[] m_TextList;
private int m_Size = 1;
// Use this for initialization
void Start () {
SetSizes();
}
public void SetSizes()
{
foreach (var item in m_TextList)
item.resizeTextMaxSize = 1000;
StartCoroutine(WaitFrame());
}
public IEnumerator WaitFrame()
{
// returning 0 will make it wait 1 frame
// need to wait a frame in order for the layout to work out the size
yield return 0;
ApplySizes();
}
public void ApplySizes()
{
// Get the first one
if (m_TextList.Length > 0)
m_Size = m_TextList[0].cachedTextGenerator.fontSizeUsedForBestFit;
// See if any are smaller
foreach (var item in m_TextList)
{
if (item.fontSize < m_Size)
m_Size = item.cachedTextGenerator.fontSizeUsedForBestFit;
}
// Apply as max size to all of the images
foreach (var item in m_TextList)
item.resizeTextMaxSize = m_Size;
}
}
Shouldn't line 37 be: if(item.cachedTextGenerator.fontSizeUsedForBestFit
Answer by RowanG1 · Apr 30, 2018 at 10:24 AM
Pretty much the same answer as Gwom, but to cater for canvas scaling. Import the script below to the relevant panel in your canvas. Assign the canvas variable in editor by drag and drop. In one of your managing scripts, create a variable:
public SetAllFontSizesToSmallestInGroup smallestFontHandler;
Then assign the SetAllFontSizesToSmallestInGroup script to the variable above in editor.
Set the Text items using the method:
smallestFontHandler.SetTextItems(newTextItems);
Then the font sizes will be set (mostly) equal and to smallest after a frame. I say mostly equal, because the resizeTextMaxSize is set 1 higher than calculated. Due to canvas scaling, and then rounding, the calculated font can be too small if 1 is not added. The end result offers a good balance between readability, and sameness.
There is a script option called "onlyCalculateSmallestSizeOnce", which can be set in the editor. If you set this true, the font size is only calculated once when you first set the text List. Even if you change the text, the original smallest font is used. This is to avoid text size jumpiness, if you change the text group content a lot over a short period of time.
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
public class SetAllFontSizesToSmallestInGroup : MonoBehaviour
{
public Canvas canvas;
int smallestFontSize;
bool hasSetAllFontSizeSameSinceBestFitDone = false;
bool returningFromSmallestFontEnumeratorYield = false;
bool haveCalculatedSmallestFontSizeAtLeastOnce = false;
public bool onlyCalculateSmallestSizeOnce = false;
List<Text> textItems;
const int fontSizeNotSet = -1;
void Start()
{
}
void Update()
{
if (NeedRecalculateSmallestFont())
{
StartCoroutine(SetTextSizesToSmallest());
}
}
bool NeedRecalculateSmallestFont()
{
if (textItems != null)
{
if (hasSetAllFontSizeSameSinceBestFitDone == false && textItems.Count > 0 && returningFromSmallestFontEnumeratorYield == false && (onlyCalculateSmallestSizeOnce == false || (onlyCalculateSmallestSizeOnce == true && haveCalculatedSmallestFontSizeAtLeastOnce == false)))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public void SetTextItems(List<Text> textItems)
{
this.textItems = textItems;
if ((onlyCalculateSmallestSizeOnce == true && haveCalculatedSmallestFontSizeAtLeastOnce == true))
{
SetFontSizeOfTextItemsAsSmallest();
}
else
{
ResetBestFitDone();
}
}
void ResetBestFitDone()
{
hasSetAllFontSizeSameSinceBestFitDone = false;
returningFromSmallestFontEnumeratorYield = false;
}
IEnumerator SetTextSizesToSmallest()
{
SetFontSizeOfTextItemsAsLarge();
returningFromSmallestFontEnumeratorYield = true;
yield return 0;
returningFromSmallestFontEnumeratorYield = false;
smallestFontSize = fontSizeNotSet;
bool updateResult = UpdateSmallestFontSizeFromTextItems();
if (updateResult == true)
{
SetFontSizeOfTextItemsAsSmallest();
hasSetAllFontSizeSameSinceBestFitDone = true;
haveCalculatedSmallestFontSizeAtLeastOnce = true;
}
else
{
smallestFontSize = fontSizeNotSet;
}
}
bool UpdateSmallestFontSizeFromTextItems()
{
foreach (Text item in textItems)
{
if (item == null)
{
return false;
}
bool updateResult = UpdateSmallestFontSizeFromOneTextItem(item);
if (updateResult == false)
{
return false;
}
}
return true;
}
bool UpdateSmallestFontSizeFromOneTextItem(Text item)
{
int fontSize = item.cachedTextGenerator.fontSizeUsedForBestFit;
if (fontSize == 0)
{
return false;
}
if (smallestFontSize == fontSizeNotSet || fontSize < smallestFontSize)
{
smallestFontSize = fontSize;
}
return true;
}
void SetFontSizeOfTextItemsAsSmallest()
{
float multiplier = 1f / canvas.scaleFactor;
foreach (Text item in textItems)
{
item.resizeTextMaxSize = (int)Mathf.Floor(smallestFontSize * multiplier) + 1; // Even if fonts won't all be identical by adding 1, it offers a good balance between readability and sameness in many cases.
}
}
void SetFontSizeOfTextItemsAsLarge()
{
float multiplier = 1f / canvas.scaleFactor;
foreach (Text item in textItems)
{
item.resizeTextMaxSize = (int)Mathf.Floor(100 * multiplier);
}
}
}
In case you want a quick way to obtain your List of Text items, this code might be useful (looks for deep children Text items:
foreach (Text textItem in gameObjectHoldingTextItemsToSetEqual.GetComponentsInChildren<Text>())
{
textItems.Add(textItem );
}
Your answer
Follow this Question
Related Questions
font size problem 1 Answer
Font Size Change. Android. 1 Answer
How to change text size in c# by user? 1 Answer
GUISkin Label Font Size Doesn't Change 1 Answer
UGUI,how to make my custom font? 1 Answer