AS3自动调整文本字段中的文本大小

时间:2015-03-02 17:48:07

标签: actionscript-3 textfield

这是我用来自动将文本放入具有固定宽度和高度的文本字段的代码:

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!');
            }
        }

    }

}

出于某种原因,只显示逗号之前的文本,之后的文本被截断。是什么原因造成的?

1 个答案:

答案 0 :(得分:1)

textWidth属性不包括所有TextField中的填充。从内存中有2px填充,所以如果你设置你的fieldWidth - = 4和fieldHeight - = 4它应该工作。

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!');
        }
    }