我在更新AS3中的动态文本字段时遇到问题。这是代码:
public class LabelFormFieldMC extends GenericFormFieldMC {
private var displayLabel:TextField;
public function LabelFormFieldMC(formField : FormField) {
super(formField);
setupLabel();
}
override public function setFieldValue() : void {
trace("LabelFormFieldMC sez: I am trying to set to value " + field.getFieldValue());
var fieldValue:String = field.getFieldValue();
var embeddedFonts : Array = Font.enumerateFonts(false);
embeddedFonts.sortOn("fontName", Array.CASEINSENSITIVE);
trace("\n----- Enumerate Fonts -----");
for(var i : int = 0;i < embeddedFonts.length;i++) {
trace(embeddedFonts[i].fontName);
}
trace("---------------------------\n");
trace("LabelFormFieldMC sez: My field value is not null " + (fieldValue!=null));
if( fieldValue != null){
displayLabel.text = fieldValue;
} else {
displayLabel.text = "";
}
trace("LabelFormFieldMC sez: My text value is " + displayLabel.text);
}
override public function setLabelWidth(labelWidth : Number):void {
super.setLabelWidth(labelWidth);
displayLabel.x = label.x + label.width + GAP_SIZE;
}
private function setupLabel():void{
var tFormat:TextFormat = new TextFormat();
tFormat.font = "Arial";
tFormat.size = 14;
tFormat.bold = true;
tFormat.align = TextFormatAlign.LEFT;
displayLabel = new TextField();
displayLabel.type = TextFieldType.DYNAMIC;
displayLabel.defaultTextFormat = tFormat;
displayLabel.border = true;
displayLabel.x = label.x + label.width + GAP_SIZE;
displayLabel.selectable = false;
displayLabel.height = 20;
displayLabel.width = 301;
this.addChild(displayLabel);
}
}
请原谅所有跟踪声明。静态GAP_SIZE之类的东西在GenericFormFieldMC中声明,它继承自。
当我查看枚举字体的跟踪输出时,我的字体是嵌入的,并且此代码在第一次更新字段值时起作用。 setFieldFalue()函数末尾的trace语句告诉我文本设置为正确的文本,但它没有显示。
我已经做了很多搜索,我仍然无法弄清楚这里发生了什么。通过销毁对象并重新创建它,我在代码的其他方面解决了这个问题,但我想知道为什么我不能只更新文本。我试过不使用文本格式并使displayLabel.embedFonts = false,但这没有帮助。如果有人有任何想法,我将非常感激。
答案 0 :(得分:1)
检查您是否嵌入了该字体所需的所有字形。我已经使用了这样的动态文本:
var f: Font;
f = new FONT_ARIAL_REGULAR; // embed font class name..
txtFormat = new TextFormat();
txtFormat.font = f.fontName;
txtFormat.color = 0x000000; // Black text :)
txtFormat.size = 12;
txtFormat.align = TextFormatAlign.LEFT;
textField = new TextField();
textField.defaultTextFormat = txtFormat;
textField.antiAliasType = AntiAliasType.ADVANCED;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.multiline = false;
textField.wordWrap = false;
textField.type = TextFieldType.INPUT;
textField.condenseWhite = true;
textField.embedFonts = true;
textField.gridFitType = GridFitType.PIXEL;
如果我没记错.. condenseWhite = true 对于设置和做一些技巧很重要..而且你应该总是从类中获取字体名称而不是硬编码字符串。
另外..调试时要简单:
不要怀疑你的技能..只要确保你的代码没有其他错误:)
答案 1 :(得分:0)
检查这个小技巧。只需将您需要的字体嵌入舞台上的虚拟文本框中并隐藏它并按照您的方式继续