文本字段中的 AS3 自动调整文本大小
AS3 auto resize text in 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!');
}
}
}
}
由于某种原因,只显示了逗号之前的文本,逗号之后的文本被截断了。这是什么原因造成的?
textWidth 属性 不包括所有 TextFields 中的填充。从内存中有 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!');
}
}
这是我用来自动将文本放入具有固定宽度和高度的文本字段的代码:
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!');
}
}
}
}
由于某种原因,只显示了逗号之前的文本,逗号之后的文本被截断了。这是什么原因造成的?
textWidth 属性 不包括所有 TextFields 中的填充。从内存中有 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!');
}
}