2

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?

Community
  • 1
  • 1
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129

3 Answers3

4

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.

Marty
  • 39,033
  • 19
  • 93
  • 162
0

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;
Rajneesh Gaikwad
  • 1,193
  • 2
  • 14
  • 30
0

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

Community
  • 1
  • 1
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • @Marty It works when I pass in the text I want emboldening. However, I am still getting out of bounds errors when I try to apply it dynamically. I think the textfield is not counting the position of the html tags; i.e. the String.indexOf function is returning the position of the and tags. Using these as start/end values gives the error as I think the textfield is removing them when it applies the bold. My string literally becomes 7 characters shorter. – Richard Parnaby-King Apr 27 '11 at 11:06