AS3 - 获取Font类对象的文本字段字段、文本、对象、Font

2023-09-08 14:30:11 作者:世间本无情

在一个混合的Flash IDE / Flex项目,我有一个文本字段,我想找回,它将与该文本字段相关的字体类对象。该文本字段住在这是在CS4 IDE中创建并加载到Flex SWF的SWF。

目前,我有code如果满足以下条件的作品:

字体出口在孩子SWF的库。 在该文本字段链接到导出的字体。 导出字体的名称是硬codeD父SWF。

code如下:

  fontClass = childSwf.loaderInfo.applicationDomain.getDefinition(CustomFont的)为类;
 
在linux中如何根据文本中的特定字段排序文本

我真正想要做的就是不会要知道导出的字体的名称。相反,我要动态地抓住任何字体的类或类的名字从文本字段。

更妙的是得到班内置字体,而无需出口的能力。

FWIW,最终目标是抓住任意文本字段,并检查它是否包含字符,它的嵌入字体不能使用字体:: hasGlyphs表明()。然而,在子SWF的字体没有登记显示在字体:: enumerateFonts()。

解决方案

您可以使用它 getTextFormat 功能的文本字段的字体名称。考虑 TXT 的文本框,然后

  VAR格式:的TextFormat = txt.getTextFormat();
跟踪(format.font);
 

经过一番研究,我发现这个解决方案,它可能会解决这个问题。

 进口API元素flash.text.TextField;
进口flash.text.TextFormat用于;

进口flash.utils.getQualifiedClassName;

进口flash.text.Font;
进口flash.display.Loader;
进口对象类型:flash.events.Event;
进口flash.net.URLRequest;

VAR字体:字体;
VAR TXT:文本字段;

VAR装载机:装载机=新的Loader();
loader.contentLoaderInfo.addEventListener(引发Event.COMPLETE,onLoaded);
loader.load(新的URLRequest(sub_fla.swf));

功能onLoaded(五:事件){
    VAR CL:类= e.currentTarget.applicationDomain.getDefinition(CustomFont的)为类;
    Font.registerFont(CL);
    跟踪(CL);

    TXT =新的TextField();
    this.addChild(TXT);
    txt.text =Moorthy;

    变种格式:的TextFormat =新的TextFormat(); // = txt.getTextFormat();
    字体=新CL();
    format.font = font.fontName;
    txt.setTextFormat(格式);

    enumerateFonts();
}

功能enumerateFonts(){
    VAR embeddedFonts:阵列= Font.enumerateFonts(假);
    embeddedFonts.sortOn(的fontName,Array.CASEINSENSITIVE);

    跟踪( - >中+ embeddedFonts.indexOf(txt.getTextFormat()字体)。);
    对于(VAR我:= 0; I< embeddedFonts.length;我++){
        字体= embeddedFonts [I]
        跟踪(embeddedFonts [+ I +]:+ font.fontName +:+字体);

        如果(txt.getTextFormat()。字体== font.fontName){
            跟踪(我的字体类是'+的getQualifiedClassName(字体)+');
        }
    }
}
 

不要忘记注册的字体得到它在 enumerateFonts 列表。否则,它获取的默认字体类不是你的自定义类。

否则,您只需加一个变量的影片剪辑(其中文本字段放置)来保存字体类或字体类名。

例如:如果是谁包含文本字段的影片剪辑,然后用

holder.fontClass = CL

而不是

Font.registerFont

您可以通过

只检索Font类

txt.parent.fontClass

如果因此在这种方式不需要 enumerateFonts 的。

In a mixed Flash IDE/Flex project, I have a TextField and I want to retrieve the Font class object that's associated with that TextField. The TextField lives in a SWF that was created in the CS4 IDE and is loaded into a Flex SWF.

Currently, I have code that works if the following criteria are met:

The font is exported in the child SWF's library. The TextField is linked to the exported font. The name of the exported font is hard-coded in the parent SWF.

Code as follows:

fontClass = childSwf.loaderInfo.applicationDomain.getDefinition("CustomFont") as Class;

What I really want to do is not have to know the name of the exported font. Instead, I want to grab either the font's Class or the Class name dynamically from the TextField.

Even better would be the ability to get the Class for built-in fonts without requiring the export.

FWIW, the end goal is to grab any arbitrary TextField and check if it contains characters that its embedded font can't show using Font::hasGlyphs(). However, fonts in child SWFs aren't registered to show up in Font::enumerateFonts().

解决方案

you can get the text field font name by using getTextFormat function of it. Consider txt is the textfield, then

var format:TextFormat = txt.getTextFormat();
trace(format.font);

After some research, I found this solution and it might solve the problem.

import flash.text.TextField;
import flash.text.TextFormat;

import flash.utils.getQualifiedClassName;

import flash.text.Font;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;

var font:Font;
var txt:TextField;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("sub_fla.swf"));

function onLoaded(e:Event){
    var cl:Class = e.currentTarget.applicationDomain.getDefinition("CustomFont") as Class;
    Font.registerFont(cl);
    trace(cl);

    txt = new TextField();
    this.addChild(txt);
    txt.text = "Moorthy";

    var format:TextFormat = new TextFormat();// = txt.getTextFormat();
    font = new cl();
    format.font = font.fontName;
    txt.setTextFormat(format);

    enumerateFonts();
}

function enumerateFonts(){
    var embeddedFonts:Array = Font.enumerateFonts(false);
    embeddedFonts.sortOn("fontName", Array.CASEINSENSITIVE);

    trace("---->"+embeddedFonts.indexOf(txt.getTextFormat().font));
    for(var i:int = 0;i<embeddedFonts.length;i++){
        font = embeddedFonts[i];
        trace("embeddedFonts["+i+"]:"+font.fontName+":"+font);  

        if(txt.getTextFormat().font == font.fontName){
            trace("My font class is '"+getQualifiedClassName(font) +"'");
        }
    }
}

Don't forget to register the font to get it in the enumerateFonts list. Otherwise it fetches the default font class not your custom class.

Or else, you can simply add a variable to the movieclip (in which the text field is placed) to hold the font class or font class name.

Eg: If holder is the movieclip who contains the textfield, then use

holder.fontClass = cl

instead of

Font.registerFont

and you can simply retrieve the font class by

txt.parent.fontClass

If so there is no need of enumerateFonts in this way.