AS3 TextField 不应用 标签

发布于 2024-11-03 21:09:49 字数 532 浏览 5 评论 0原文

我使用 AS3 动态创建、调整大小、定位和格式化文本字段。我从 xml 文件的内容动态设置文本字段的内容。我正在尝试使用粗体标签,但发现它不起作用。经过一番搜索后,我能想到的最好的方法是“Flash CS4 标签与 htmlText”。底线:我必须嵌入一个加粗的字体。

举个例子,假设我想使用 Tahoma。在我的 .fla 文件(使用 Flash CS4)中,我嵌入了 Tahoma 并将其导出以在动作脚本中使用。这让我可以在文本字段中使用 Tahoma 作为字体。如果我尝试使用 b 标签(textfield.htmlText="not bad, bold";),则粗体文本不会加粗。基于上述问题,我现在也嵌入了 Tahoma 的粗体版本。

如何将 Tahoma 的粗体版本与 Tahoma 的常规版本链接起来,以便在使用粗体标签时在文本字段中获得粗体文本?

Using AS3 I am dynamically creating, sizing, positioning and formatting a textfield. I am dynamically setting the content of the textfield from the contents of an xml file. I am trying to use the bold tag and noticed that it is not working. After a bit of searching the best I could come up with is "Flash CS4 tag in with htmlText". Bottom line: I have to embed an emboldened fontface.

As an example, let's say I want to use Tahoma. In my .fla file (using Flash CS4) I embed Tahoma and export it for use in actionscript. This lets me use Tahoma as a font in my textfield. If I try to use the b tag (textfield.htmlText="not bold, <b>bold</b>";) the bold text does not get emboldened. Based on the above question I have now embedded the Bold version of Tahoma as well.

How do I link the bold version of Tahoma with the regular version of Tahoma so that when using the bold tag I get bold text in my textfield?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-11-10 21:09:49

要设置文本的某些部分为粗体,您必须采用烦人的方式: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);

这将输出以下内容:this 是粗体。

编辑
这应该会让事情变得更容易:

/**
 * 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);

产量:我喜欢茶和展示包

For setting certain sections of text bold you have to do it the annoying way: TextFormats.

Like so;

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);

This would output the following: this is bold.

EDIT
This should make it easier:

/**
 * 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);

EDIT
What up.

/**
 * 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);

Yields : I like iced tea and showbags.

安静 2024-11-10 21:09:49

基于 @Marty Wallace 的回答:

我已经在代码的前面设置了文本格式。它加载字体、大小等。

我正在做的是将字体应用到我的文本字段,然后如果字符串包含粗体标签,则将 textformat 粗体属性设置为 true。将代码放置在 setTextFormat 上方会使整个文本字段变为粗体。由于某种原因,将其设置在文本字段下方会使标记的文本变为粗体。

textfield.setTextFormat(textformat);
if(xmlText.search('<b>')){
    textformat.bold = true;
}

[编辑]

不需要 if:

textfield.setTextFormat(textformat);
textformat.bold = true;

[编辑]

您还需要嵌入粗体字体才能使其工作。我把它从我的库中删除了,并且无法让粗体再次工作>。<在我的 tahoma 示例中,字体名称需要为“Tahoma 粗体”。

因此,如果我想使用 ,现在我需要嵌入所有字体的粗体和斜体版本。 在我的 htmlText 中

Based on @Marty Wallace's answer:

I have set up my textformat earlier in the code. It loads the font, size, etc.

What I am doing is applying the font to my text field, then setting the textformat bold attribute as true if the string contains a bold tag. Placing the code above the setTextFormat makes the entire textfield bold. For some reason setting it below the textfield makes just the tagged text bold.

textfield.setTextFormat(textformat);
if(xmlText.search('<b>')){
    textformat.bold = true;
}

[edit]

Don't need the if:

textfield.setTextFormat(textformat);
textformat.bold = true;

[edit]

You also need the emboldened font embedded for this to work. I removed it from my library and couldn't get the bold to work again >.< In my tahoma example, the font name needs to be 'Tahoma bold'.

So now I need to embed the emboldened and italicised versions of all my fonts if I want to use <b> and <i> in my htmlText

假情假意假温柔 2024-11-10 21:09:49

在 Flash 中,放置实例名称为“myTextField”的动态文本字段,并将以下代码放置在时间轴上。 (仅用于测试目的,否则不建议使用时间线代码),

 var str:String = <![CDATA[ <font face='Arial' size='18' align='left'> This is my <b>BOLD</b>  text </font>]]>;
 myTextField.htmlText = str;

In Flash, place Dynamic TextField with instance name "myTextField" and place the following code on timeline. (For testing purpose only otherwise timeline code is not recommended),

 var str:String = <![CDATA[ <font face='Arial' size='18' align='left'> This is my <b>BOLD</b>  text </font>]]>;
 myTextField.htmlText = str;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文