views:

1181

answers:

4

How can I make my strings and text attributes bold in my actionscript code?

I'm working with the code behind pattern so i have an mxml component with a text attribute. I then have my actionscript component where I concatenate three text attribute and set them as the text property on the mxml text component.

I want to be able be flexible with the styles of each text field I concatenate. I want the first text bold and the last bold however. Any ideas?

+1  A: 

If you want to use different font weights for a text, you have no other choice than creating 2 text components (Label, Text, ...).

If you use external fonts, be sure to embed both normal and bold fonts :

@font-face
{
    src:     url("calibri.ttf");
    fontFamily:    calibri;
    advanceAntiAliasing:                    true;
    fontWeight:    normal;
}

@font-face
{
    src:   url("calibrib.ttf");
    fontFamily:  calibri;
    fontWeight:  bold;
}

and set some styles in your css like :

.calibri16
{
    font-size: 16;
    font-weight:normal;
    font-family:calibri;
    color: #666666;
}

.calibri16b
{
    font-size: 16;
    font-weight:bold;
    font-family:calibri;
    color: #666666;
}

You can then set the styleName property of your text components :

<HBox>
<Label id="myLabel" styleName="calibri16b" text="This is bold" />
<Label id="myLabel2" styleName="calibri16" text="and regular" />
</HBox>

In Action Script :

myLabel.styleName = "calibri16b";
myLabel2.styleName = "calibri16";
PeZ
A: 

Hi, here is the sample application. I assume you have the assets directory with century.ttf file in it:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
 <mx:Style>
  @font-face {
   src: url("assets/century.ttf");
   fontFamily: Century;
   fontStyle: normal;
   fontWeight: normal;
  }
  .myClass {
   fontFamily: Century;
  }
 </mx:Style>
 <mx:Label text="Different font" styleName="myClass"  />
</mx:WindowedApplication>

Not all fonts expose all variants. Some fonts will give you normal only, some give you all: normal, bold, italic. Hope this helps.

radekg
A: 

In ActionScript if you don't want a TextFormat, that is:

myTextFormat = new TextFormat();
myTextFormat.bold = true;
myTextField.setTextFormat(myTextFormat);

...you can also use htmlText:

myTextField.htmlText='<b>Bold</b> not bold <b>Bold again!</b>';
geocar
A: 

I have Arial Bold and Arial Regular on the stage with embed fonts, and I have them both in the Library. Now if I fill a html textfield with "boldregular" the bold text will not display. What am I missing?

Ted Brunninkhuis