Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Norax · Jul 09, 2014 at 09:40 AM · c#uitext

How to know the font height in pixels (and other attributes) of a text?

I end up making a function that calculates the height of a text inside of a multi-line element. Taking account the height of the font, the margin of the element and the space between lines, im pretty sure I could improve my guess (kind of random at the moment). But I have no idea how to access that info.

Also I have a random guess to fit the width, which works quite accurate at the moment. But since the font is not monospace, it could have some error.

Here the code:

 private int FormatTextForUIElements(string inputText, out string resultText, int elementWidth)
     {
         if(!inputText.Contains("\n"))
         {
             string[] aWord = inputText.Split (' ');
             string line = string.Empty;
             resultText = string.Empty;
 
             foreach(string word in aWord)
             {
                 // Fits the word in the line?
                 if((line.Length + 1 + word.Length)*6.5 < elementWidth)
                 {    
                     line += " " + word;
                 }
                 else
                 {    
                     // Its result empty?
                     if(resultText.Equals(string.Empty))
                     {
                         resultText = line;
                     }
                     else
                     {    
                         resultText += "\n" + line;
                     }
                     line = word;
                 }
             }
             // Its result empty?
             if (resultText.Equals(string.Empty))
             {    
                 resultText = line;
             }
             else
             {    
                 resultText += "\n"  + line;
             }
 
         }
         else
         {
             resultText = inputText;
         }
         int result = resultText.Split ('\n').Length;
         return  result * 17 + 6;
     }

In case you want to try it:

 public int elementW = 130; // public so you can play with it on the editor
 [...]
 string formatedText = string.empty;
 string text = "Some long text that will be formated to fit the UI element";
 int heightForTextBox = FormatTextForUIElements (text, out formatedText,elementW);
 string test = GUI.TextArea (new Rect (0, 0, elementW, heightForTextBox), formatedText);

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Jul 09, 2014 at 03:32 PM

GUIStyle.CalcSize.

It requires you to make a "fake" GUIStyle, using the font and size you have. But then you can just measure a word.

There are some small issues. I use one font where, for words like "To", a TextMesh tucks the "o" under the right bar of the "T", but calcSize doens't know that, so returns a too-large width.

Comment
Add comment · Show 2 · 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 Norax · Jul 09, 2014 at 11:14 PM 0
Share

For non-monospace fonts, it will requiere a personalized function to get the real width.

But right know I am more concerned to get a more accurate height (since I already made my own function). I expect to be somewhere a function like "fontsize" or something (and not the "scalable" thing some components have) to call, and also to access the style.somecontrol.margin But didnt found yet in documentation something like that.

avatar image KazYamof · Nov 03, 2015 at 06:11 PM 0
Share

This approach doesn't work to Unity 4.6+ Text component. There is anyway to measure the pixel size of a Text in Unity 4.6+?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

,InputField backspace returns already send text 0 Answers

How to measure the width of a string? 0 Answers

How to use special symbols 1 Answer

Display fixed number of words in textbox at a time 1 Answer

Problems to change text from a child in a child 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