1

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?

2
  • That resizing cannot work if multiline is true. Commented Mar 2, 2015 at 18:00
  • @BotMaster even when I set multiline to false, it doesn't work Commented Mar 2, 2015 at 19:40

1 Answer 1

1

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!'); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.