AS3 TextField未应用标记

| 使用AS3,我可以动态创建,调整大小,定位和格式化文本字段。我正在从xml文件的内容动态设置文本字段的内容。我正在尝试使用粗体标签,但发现它不起作用。经过一番搜索,我能想到的最好的是\“带有htmlText的Flash CS4标记\”。底线:我必须嵌入一个加粗的字体。 例如,假设我要使用Tahoma。在我的.fla文件中(使用Flash CS4),我嵌入了Tahoma并将其导出以用于actionscript。这使我可以将Tahoma用作文本字段中的字体。如果我尝试使用b标记(
textfield.htmlText=\"not bold, <b>bold</b>\";
),则粗体字不会变粗。基于上述问题,我现在也嵌入了Tahoma的加粗版本。 如何将Tahoma的粗体版本与常规版本的Tahoma链接起来,以便在使用粗体标签时在文本字段中获得粗体文本?     
已邀请:
为了将文本的某些部分设置为粗体,您必须使用恼人的方式:TextFormats。 像这样
var t:TextField = new TextField();
t.text = \"this is bold\";

var f:TextFormat = new TextFormat();
f.bold = true;

t.setTextFormat(f, 0, 4); // start at character 0, end at character 4

addChild(t);
这将输出以下内容:这是粗体。 编辑 这应该使它更容易:
/**
 * Render a given portion of a String as bold
 * @param field The target TextField
 * @param needle The section of text to render bold
 */
function bolden(field:TextField, needle:String):void
{
    var tf:TextFormat = new TextFormat();
    tf.bold = true;

    var pos:uint = field.text.indexOf(needle);
    field.setTextFormat(tf, pos, pos + needle.length);
}


// example below
var t:TextField = new TextField();
t.text = \"i like iced tea\";

bolden(t, \"iced\");

addChild(t);
编辑 怎么了。
/**
 * Apply <b> tags
 * @param field The target TextField
 */
function bolden(field:TextField):void
{
    var tf:TextFormat = new TextFormat();
    tf.bold = true;

    var pos:int = 0;
    var cls:int = 0;

    while(true)
    {
        pos = field.text.indexOf(\"<b>\", pos);
        cls = field.text.indexOf(\"</b>\", pos);

        if(pos == -1) break;

        field.setTextFormat(tf, pos+3, cls);
        pos = cls;
    }
}


// example below
var t:TextField = new TextField();

t.width = stage.stageWidth;
t.htmlText = \"i like <b>iced</b> tea and <b>showbags</b>\";

bolden(t);

addChild(t);
收益:我喜欢冰茶和展示袋。     
基于@Marty Wallace的答案: 我已经在代码前面设置了textformat。它加载字体,大小等。 我正在做的是将字体应用于文本字段,然后在字符串包含粗体标记的情况下将textformat粗体属性设置为true。将代码放在setTextFormat上方会使整个文本字段变为粗体。由于某种原因,将其设置在文本字段下方会使标记的文本变为粗体。
textfield.setTextFormat(textformat);
if(xmlText.search(\'<b>\')){
    textformat.bold = true;
}
[编辑] 不需要if:
textfield.setTextFormat(textformat);
textformat.bold = true;
[编辑] 您还需要嵌入嵌入的字体才能正常工作。我将其从库中删除,无法再次使用粗体>。<在我的tahoma示例中,字体名称必须为\'Tahoma bold \'。 因此,如果我想在htmlText中使用,现在需要嵌入所有字体的加粗斜体版本。     
在Flash中,使用实例名称\“ myTextField \”放置Dynamic TextField并将以下代码放置在时间轴上。 (仅出于测试目的,否则不建议使用时间轴代码),
 var str:String = <![CDATA[ <font face=\'Arial\' size=\'18\' align=\'left\'> This is my <b>BOLD</b>  text </font>]]>;
 myTextField.htmlText = str;
    

要回复问题请先登录注册