Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6087555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:53:01+00:00 2026-05-23T11:53:01+00:00

I have a text field with a set height and width and that allows

  • 0

I have a text field with a set height and width and that allows word wrapping. When I add text to it, I dynamically change the font size until the height of the text is smaller than the height of the box, allowing it to properly fit. The problem I am running into is that the box is fairly thin by necessity, and some longer words (like “Australia”) are broken during the word wrap. So, perhaps “Australi” will show up on the first line, and “a” on the second line. However, because they satisfy the height requirement, they aren’t shrunk any more. I need a way to detect this and continue shrinking these cases so they fit without the break. Here is the code for dynamically shrinking the text to fit the height:

text.wordWrap = true;
text.autoSize = TextFieldAutoSize.CENTER;

while(text.height > textBoxVal) {
    myFormat.size = Number(myFormat.size) - 2;
    text.defaultTextFormat = myFormat;
    text.setTextFormat(myFormat);
}

This works fine. The problem with my approach to the next step (shrinking if horizontal constraints are not met because of a word being split) is that by leaving text.wordWrap set to true, the longer words will still break and never be resized, but setting it to false means that the other boxes that were fine before because of word wrap being on (like “United States”) are now too long on a single line, and are shrunk when they shouldn’t be. For what it’s worth, here’s my code to shrink horizontally (immediately follows above code):

text.wordWrap = false;
text.autoSize = TextFieldAutoSize.LEFT;
while(text.width > textBoxVal) {
    myFormat.size = Number(myFormat.size) - 2;
    text.defaultTextFormat = myFormat;
    text.setTextFormat(myFormat);
}

Is there a better way to go about this? Perhaps some way to force ActionScript not to break words, etc?

Edit: To give an example, please look at the screenshot below:

enter image description here

This is using only the first code block, which ensures that the text fits into the box’s vertical constraints. You can see that some boxes (like the first for the US) are fine, whereas some of the smaller ones (like the last few) are split. Using my second code block to resize the horizontal constraints makes no changes if word wrap is true, and will make all boxes (including ones that are fine, like the US) smaller than needed if word wrap is set to false.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:53:02+00:00Added an answer on May 23, 2026 at 11:53 am

    If your text wraps to another line, you should be able to
    check maxScrollV.

    My guess below:

    while((text.height > textBoxVal || text.maxScrollV > 1) && myFormat.size > 1) {
        myFormat.size = Math.max(1, Number(myFormat.size) - 2);
        text.defaultTextFormat = myFormat;
        text.setTextFormat(myFormat);
    }
    

    EDIT

    What I suggest is

    1. add text into field word by word
    2. if maxScrollV changes, you get an automatic word wrap
    3. insert implicit line break
    4. resize the text to fit desired height
    5. repeat until all words are not there

    Here is some code to illustrate that:

    private function fitText (text:String, rect:Rectangle, tf:TextField, format:TextFormat) : void
    {
        // split text by whitespace
        var heap:Array = text.split(/\s+/);
    
        var size:uint = format.size;
        var maxScroll:uint = 1;
        var text:String;
        var word:String;
    
        // make sure tf is set up correctly
        tf.defaultTextFormat = format;
        tf.setTextFormat(format);
        tf.multiline = true;
        tf.wordWrap = true;
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.width = rect.width;
    
        for (var i:uint = 0; i<heap.length;i+=1) {    
            // insert text word by word
            word = heap[i];
            text = tf.text;
            if (i) {
                tf.text = text + " " + word;
            } else {
                tf.text = word;
            }
            if (tf.maxScrollV > maxScroll) {        
                // the last word you inserted increased number of lines 
                // and possibly got broken
    
                // increase scroll by 1
                // if scroll actually increased by more than 1,
                // we got word broken multiple times
                maxScroll += 1;
    
                // insert implicit line break, if not the first word
                if (i) {
                    tf.text = text + "\n" + word;
                }
            }
    
            // do the resizing routine, if needed
            while (tf.height > rect.height || tf.maxScrollV > maxScroll) {
                // we also check tf.maxScrollV,
                // as it can be no more than maxScroll.
                // otherwise we'll get words crippled    
    
                /*resizing code here*/
            }
        }
    
        // restore original size
        format.size = size;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my hello world application, i have a button and a text field set
I have a set of data that contains garbled text fields because of encoding
I have a text field (flash.display.textField) for editing, but the default font does not
I have some dynamic text contained in a div that is set to whatever
I have form that has a restricted size and the overflow style set to
I have a formPanel set up with a text field. When a button is
I have a text field and am looking to have if be valid for
If I have a text field with SWT, how can I get the field
Hi I have a text field which I would like to bind to a
I am making a text based game in Java. I have a text field,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.