【问题标题】:AS3 auto resize text in textfieldAS3 自动调整文本字段中的文本大小
【发布时间】:2015-05-03 04:29:45
【问题描述】:

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

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

    }

}

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

【问题讨论】:

  • 如果 multiline 为真,则无法调整大小。
  • @BotMaster 即使我将multiline 设置为false,它也不起作用

标签: actionscript-3 textfield


【解决方案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!');
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多