Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FrankyTroxy95 · Jan 19, 2017 at 09:09 PM · font size

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EDevJogos · Jan 19, 2017 at 09:29 PM 0
Share

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.

avatar image FrankyTroxy95 · Jan 20, 2017 at 09:02 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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;
     }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacobfrank · Jul 25, 2019 at 10:02 PM 0
Share

Shouldn't line 37 be: if(item.cachedTextGenerator.fontSizeUsedForBestFit

avatar image
0

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);
         }
     }
 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RowanG1 · Apr 30, 2018 at 11:03 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges