Basic TextFormat change is doing nothing - actionscript-3

Code:
var BoldTextFormat:TextFormat = new TextFormat();
BoldTextFormat.bold = true;
weapons[player.activeWeapon].weaponName.defaultTextFormat = BoldTextFormat;
This seems very simple, but it is not doing anything! The text is not becoming bold at all.
Using weapons[player.activeWeapon].weaponName.setTextFormat(BoldTextFormat); does nothing also.
These traces:
trace(weapons[player.activeWeapon].weaponName);
trace(weapons[player.activeWeapon].weaponName.defaultTextFormat);
trace(BoldTextFormat);
Result in this output:
[object TextField]
[object TextFormat]
[object TextFormat]
And this is the text field I am trying to make bold:
I have embedded the bold font:
What could possibly be wrong? There are no errors. Tracing the text field's text shows the correct text, so that can't be the problem.

You might need to embed the bold font, your image shows 'Regular' font.
Ok, I figured out the magic here. Likely why it started working for you is that you actually set the bold font for your field.
But here's the magic. In the library, right-click on the bold font and choose properties. Then go to the actionscript settings and enable :
export for actionscript
Next, in your code you need to add this line :
BoldTextFormat.font = "Times New Roman Bold";
That should work.
Sadly, the .bold of the TextFormat doesn't seem to automatically find the bold version.

Try :
weapons[player.activeWeapon].weaponName.setTextFormat(BoldTextFormat);
defaultTextFormat applies to text that is added after you set the format, if you want to change the format of text that is already set you should use setTextFormat

Related

Japanese text in TextField with embedded font

In my ActionScript 3 project I use TextField with embedded font set to true and isHTML set to true. Everything works fine when I display latin characters.
But when I want to display Japanese characters they do not appear. Actually it is expected to be like this because Japanese font is not embedded.
Is there is a way to fall back to system font if embedded font doesn't have glyphs for certain characters?
Try to use hasGlyphs("..."); on a Font instance. I had some problems with it but in general it works alright, hopefully it works for Japanese too. Then you can simply use _sans if there is no japanese glyphs.
Edit:
I have no way to test this now but I assume something like this would work:
import flash.text.Font;
var myFont:Font = new MyFont(); // Embedded font in library, linkage set to 'MyFont'
if(myFont.hasGlyphs("Hello!")) {
field.embedFonts = true; // Not sure whether this is necessary in case of HTML text
field.htmlText = "<p><font color='#ff0000' face='" + myFont.fontName + "'>Hello!</font></p>";
}
else {
field.embedFonts = false;
field.htmlText = "<p><font color='#ff0000' face='_sans'>Hello!</font></p>";
}

Unicode that is not recognized properly

I'm using in Unicode in my application using
for(var i:int=0;i<16;i++) {
for(var j:int=0;j<16;j++) {
button = new Button();
button.x = j*35+10;
button.y = i*20+10;
button.height = 21;
button.width = 35;
button.setStyle("borderColor","red");
button.setStyle(" fontWeight","bold");
button.label= String.fromCharCode(0x2190+16*i+j);
button.addEventListener(MouseEvent.CLICK,greekalpha_clickHandler);
vgr.addElement(button);
}
}
But when I run this code, some symbols are recognized properly and others are not
Hilighted words are recognized properly and others are not. What mistakes have I made in this code?
(I'm just reposting the apparent answer to get this question off the unanswered list. The original answerer(s) in the comments are more than welcome to post instead. I also know the given answer should work, based on my own experience.)
In order to ensure the Unicode symbols show up properly, you need to embed a Unicode font, such as Arial Unicode MS or Lucida Sans Unicode, and set your text objects to that font (it doesn't happen automatically).
Then, turn on anti-aliasing, either via advancedAntiAliasing = true; or by setting the Anti-alias property in Flash Professional to "Anti-Alias for readability".
Any issues beyond this, I presume, are device-specific.

how do I change a TextArea component's font size in Flash using AS3?

I want to change the font size of Flash's TextArea component which is currently on the stage using AS3.
I tried linking a TextFormat object to the TextAera by its instance name, but I get this error:
Scene 1, Layer 'actions', Frame 1, Line 44 1061: Call to a possibly undefined method setTextFormat through a reference with static type fl.controls:TextArea.
Here is the AS3 code:
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = "Arial";
myTextFormat.size = 10;
myTextFormat.color = 0xffffff;
my_textArea.setTextFormat(myTextFormat)
Try this snippet:
myTextArea.setStyle("textFormat", myTextAreaFormat);
What I discovered is that the textFormat needs to be set prior to adding the text. When you do that, the setTextFormat command works fine. However, if you define the associated textFormat after its associated textField has been defined, it breaks and the font size isn't set.
define a wrapper (in mine, I used a sprite) for the textField
define textFormat for your textBox (in mine I defined "textBoxFormat")
define your text field (in mine, I used "textBox")
add textBox to it's wrapper
Finally, set the textFormat ==> textBox.setTextFormat(textBoxFormat);
add the wrapper from step 1 to the stage (or other element).
To be honest, I'm not 100% sure the first step is required, but I found it beneficial to wrap the textField, just to be safe. 34 years of programming out the door with my introduction to Adobe's weird "standards". Wish there was a practical alternative, but there doesn't seem to be.
Although One answer is accepted, That doesn't work for me and I found this solution:
import mx.controls.TextArea;
_global.styles.TextArea.setStyle("fontFamily", "Arial");
_global.styles.TextArea.setStyle("fontSize", 30);
PS: If you need in AS2 .

AIR Mobile Embedded Font

I am trying to change the default font of a text field in an AIR Mobile app.
What I dos first embed the font using this line:
[Embed(source='C:/WINDOWS/Fonts/ARIAL.TTF', fontName="_Arial", mimeType="application/x-font-truetype")]
After that, I create a TextFormat with this font: style.font = "_Arial"; and set it to the text field: tf.setTextFormat(style);
Sadly, the font does not change.
Anyone knows what I am doing wrong?
You need to use:
tf.defaultTextFormat = style;
There are differenced between defaultTextFormat and setTextFormat
setTextFormat
defaultTextFormat
UPDATE:
not sure why this is not working what you have there, but i managed to make it working, just replace the font embedding line with this:
[Embed(systemFont="Arial", fontName="_Arial", mimeType="application/x-font-truetype", embedAsCFF="false")]
Did you set the textfield.embedfont = true. ?

Can't embed multiple fonts and use them in same textfield - cs5, as3

having a bit of trouble getting embedded fonts to work. The inconsistency with this is driving me crazy. I've gotten this to work before but not since going to cs5.
My set up is as follows:
I have a dynamic textfield on stage named "tf".
The following code populates the textfield and attempts to style one word, "love" in the sentence with a different font.
_style = new StyleSheet();
var styleObj = new Object();
styleObj.color = '#FF0000';
styleObj.fontFamily = 'Futura Bold Oblique';
_style.setStyle('.otherText', styleObj);
tf.styleSheet = _style;
tf.htmlText = 'I <span class="otherText">love</span> it when stuff works!';
The font is embedded and set to export for actionscript with the export name Font1. The word "love", which is supposed to be displayed as the font Futura Bold Oblique just disappears. It makes no difference if I use the font screen name listed in the properties panel for that font or if I use the export instance name I gave it.
My sentence looks like : I it when stuff works! As you can see, "love" is gone. Come on people, show me some love! lol.
Anyone have an idea why this isn't working. This is highly essential to my daily work and its driving my crazy!! Thx!
I have been playing around with this since posting, hoping that maybe that specific font was causing the trouble, but its not the font. I tried using different styles of Helvetica Neue as separately embedded fonts and got the same result.
Fixed it, sort of. using enumerateFonts. This lists the fonts' names as Flash sees them. Copy and paste from the output window, and use that for the stylesheet fontFamily property. It still seems that Flash thinks some different styles from the same family, like Futura Bold and Futura Bold Oblique are the same; in my case it listed them both as Futura Book. In order to get the Oblique text in there I had to embed two separate versions of the font, Futura and Futura BT.
private function getFontNames():void
{
var fontArray:Array = Font.enumerateFonts(false);
fontArray.sortOn("fontName", Array.CASEINSENSITIVE);
for(var i:int = 0; i<fontArray.length; i++)
{
trace(fontArray[i].fontName);
}
}