c4isgonnablow
c4isgonnablow

Reputation: 61

AS3 auto resize text in textfield

This is the code I use to automatically fit text into a textfield with a fixed width and height:

package  {
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.Sprite;

    public class AutoResizeText extends Sprite{

        public function AutoResizeText() {
            var textField:TextField = new TextField();
            var textFormat:TextFormat = new TextFormat();

            textField.text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
            textField.wordWrap = true;
            textField.multiline = true;
            textField.width = stage.stageWidth/2;
            textField.height = stage.stageHeight/8;
            textField.x = stage.stageWidth/4;
            textField.y = stage.stageHeight/4;
            textField.border = true;

            textFormat.size = 10;
            textField.setTextFormat(textFormat);

            autoResizeTextField(textField, textField.width, textField.height, false, true);

            addChild(textField);
        }

        public function autoResizeTextField(textField:TextField, fieldWidth:int, fieldHeight:int, bOneLine:Boolean = false, bResizeBigger:Boolean = false):void{
            //checks if wordwrap is set to true
            if(textField.wordWrap == true){
                var textFormat:TextFormat = textField.getTextFormat();  

                if(textField.textWidth > fieldWidth || textField.textHeight > fieldHeight || (bOneLine && textField.numLines > 1)){
                    while(textField.textWidth > fieldWidth || textField.textHeight > fieldHeight || (bOneLine && textField.numLines > 1)){
                        textFormat.size = int(textFormat.size) - 1;
                        textField.setTextFormat(textFormat);
                    }
                } else if(textField.textWidth < fieldWidth && textField.textHeight < fieldHeight && bResizeBigger){
                    while(textField.textWidth < fieldWidth && textField.textHeight < fieldHeight && !(bOneLine && textField.numLines > 1)){
                        textFormat.size = int(textFormat.size) + 1;
                        textField.setTextFormat(textFormat);
                    }

                    if(textField.textWidth > fieldWidth || textField.textHeight > fieldHeight || (bOneLine && textField.numLines > 1)){
                        textFormat.size = int(textFormat.size) - 1;
                        textField.setTextFormat(textFormat);
                    }
                }
            } else{
                //gives an error
                throw new Error('wordWrap needs to be set to true in order to auto resize a textfield!');
            }
        }

    }

}

For some reason, only the text until the comma is shown, the text after that is cut off. What causes this?

Upvotes: 1

Views: 1250

Answers (1)

Glen Blanchard
Glen Blanchard

Reputation: 976

The textWidth property doesn't include the padding in all TextFields. From memory there is 2px padding so if you set your fieldWidth -= 4 and fieldHeight -= 4 it should work.

public function autoResizeTextField(textField:TextField, fieldWidth:int, fieldHeight:int, bOneLine:Boolean = false, bResizeBigger:Boolean = false):void{
        // Allow for padding of TextFields
        fieldWidth = Math.max(0, fieldWidth - 4);
        fieldHeight = Math.max(0, fieldHeight - 4);
        //checks if wordwrap is set to true
        if(textField.wordWrap == true){
            var textFormat:TextFormat = textField.getTextFormat();  

            if(textField.textWidth > fieldWidth || textField.textHeight > fieldHeight || (bOneLine && textField.numLines > 1)){
                while(textField.textWidth > fieldWidth || textField.textHeight > fieldHeight || (bOneLine && textField.numLines > 1)){
                    textFormat.size = int(textFormat.size) - 1;
                    textField.setTextFormat(textFormat);
                }
            } else if(textField.textWidth < fieldWidth && textField.textHeight < fieldHeight && bResizeBigger){
                while(textField.textWidth < fieldWidth && textField.textHeight < fieldHeight && !(bOneLine && textField.numLines > 1)){
                    textFormat.size = int(textFormat.size) + 1;
                    textField.setTextFormat(textFormat);
                }

                if(textField.textWidth > fieldWidth || textField.textHeight > fieldHeight || (bOneLine && textField.numLines > 1)){
                    textFormat.size = int(textFormat.size) - 1;
                    textField.setTextFormat(textFormat);
                }
            }
        } else{
            //gives an error
            throw new Error('wordWrap needs to be set to true in order to auto resize a textfield!');
        }
    }

Upvotes: 1

Related Questions