Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
2
Question by SomeSortOfHuman · Jul 18, 2015 at 10:59 AM · unity 5uitext

[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:

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:

play mode

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"?

editor.png (66.9 kB)
playmode.png (4.1 kB)
Comment
Add comment · Show 1
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 SomeSortOfHuman · Jul 18, 2015 at 01:50 PM 0
Share

You don't have to post a full answer. A link is enough.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

  1. Get the font size selected by Unity to do the best fit.

  2. Get the height of the text component.

  3. If the height is at least twice the font size, shrink the component to the fontSize value.

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

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 danipisca07 · Aug 24, 2015 at 02:00 PM 0
Share

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

avatar image
3

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.

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 SweatyChair · Jun 12, 2018 at 06:14 AM 0
Share

Quite a hacky way but it works!

avatar image roointan · Jan 26, 2020 at 03:58 PM 0
Share

Thanks! it works.

avatar image
1

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.

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 SomeSortOfHuman · Jul 18, 2015 at 09:34 PM 0
Share

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.

avatar image Eudaimonium · Jul 18, 2015 at 10:54 PM 0
Share

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.

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

6 People are following this question.

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

Related Questions

Ui text to string ? 1 Answer

Unity UI Text - Always show 5 last lines 0 Answers

Unity UI Text with markup tags - “String too long for TextMeshGenerator” 0 Answers

Text Highlight word by word when background voice is playing. 0 Answers

UI Text VS TextMeshPro UGUI 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