AS3 change only font of TLFTextField - actionscript-3

To change font in TLFTextField I need to do something like this:
var textFormat:TextFormat = new TextFormat();
textFormat.font = "NewFontName";
textField.defaultTextFormat = textFormat;
and it works ok, but I loose all other previous properties of textField like color, font size or align. How can I overcome this? I don't want new TextFormat, I want to change font only in existing one. I set color and align in fla file and I want to change font in AS code.

Easiest approach is:
var format: TextFormat = textField.defaultTextFormat;
format.font = "Consolas";
textField.setTextFormat(format);
Or you can store reference on TextFormat, if you change it often

Related

Increase size of text on click on button

How do I increase size of a dynamic text on click of a button in AS3, Adobe flash?
I have a dynamic text box (instance name is damodara).
I tried using the following code but it didn't work.
text_big.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(e:MouseEvent):void
{
damodara.size += 4;
}
I would be glad if you could help me.
Use set setTextFormat like this:
First, you need to have aTextFormat object. Like this:
var tf:TextFormat = new TextFormat();
then set the properties of that like this:
tf.size = 5; // or whatever you want here
then apply this format to your text:
myTextField.setTextFormat(tf);
Then, in the code you posted, replace size+=5 with damodara.setTextFormat(tf);
Then to change it back you could either have a different TextFormat with a different size property or change the size property of this TextFormat and reapply the format doing setTextFormat again.
You cannot increase the size of the text, instead increase the font size while you click the button , then you are good to go.
textFormat = youtTextField.getTextFormat();
textFormat.size = int(textFormat.size)-1;
youtTextField.setTextFormat(textFormat);

Why can't set "defaultTextFormat" directly?

for example,we can set graphics of a shape directly(without creating an external Graphics variable):
var my_shape:Shape=new Shape();
my_shape.graphics.beginFill(0);
but that's not same as defaultTextFormat
the below code doesn't work:
var my_text:TextField=new TextField();
my_text.defaultTextFormat.size=47;
typing dot after defaultTextFormat,the code hint of text format appears and there is no compiler error but still doesn't work
we must create an external TextFormat variable:
var my_text:TextField=new TextField();
var my_format:TextFormat=new TextFormat();
my_format.size=47;
my_text.defaultTextFormat=my_format;
but why can't set directly?
I don't like a lot of variables.
after that,explain the difference between textFormat and Graphics.
Thanks for your help.
When you access/read a TextField's defaultTextFormat property (which is what's happening in the line my_text.defaultTextFormat.size=47;), you end up getting a whole new object returned. Eg, it creates a new TextFormat and returns that.
Here is an example to illustrate:
var tf:TextFormat = new TextFormat();
textField.defaultTextFormat = tf;
trace(tf == textField.defaultTextFormat) //false
The TextField doesn't know anything about the TextFormat it returns from defaultTextFormat. So when you change it, it doesn't update anything automatically because it has no scope inside the TextField that generated it.
In order to see the change, you have to reassign the whole object, and
then reassign the text (if you've already set the text).
This unfortunately means you'll have do it like in your second example.
It's probably some kind of an efficiency thing under the hood to help prevent memory leaks and the like.
Here are some examples to consider further:
var txt:TextField = new TextField();
addChild(txt);
var tf:TextFormat;
txt.text = "hi"; //default formatting;
tf = txt.defaultTextFormat; //get the default formatting, which actually returns a brand new object
tf.color = 0xFF0000; //make it red;
//nothing has changed visually
txt.defaultTextFormat = tf; //this won't update it either
//nothing has changed visually
txt.text = txt.text; //now that we've 'changed' the text, you'll see red
my_text.defaultTextFormat = my_format;
my_format is the defaultTextFormat of your textField my_text. defaultTextFormat is a property of your TextField (which value is my_format).
my_format.size = 47;
47 is the size of your TextFormat my_format. size is a property of your TextFormat (which value is 47).
my_text.defaultTextFormat.size = 47;
...but size has never been a property of a defaultTextFormat.
So you cannot put properties directly on the defaultTextFormat. What
you need to do is to make a text format, set the properties, THEN set
defaultTextFormat = myTextFormat.
Adobe help about defaultTextFormat.

How to use a font embedded in a SWC

I exported a SWC from Flash CS3 with a font embedded.
Now I want use it in some TextFields, but I don't know how to use the font.
Done! Here's my solution:
[Embed(source="res/guardanapo.otf", fontName="guardanapo", fontFamily="guardanapo", unicodeRange="U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E", advancedAntiAliasing="false", embedAsCFF="false")]
public static const fonte:Class;
I wanted to embed the font from SWC, but now it isn't necessary anymore. But answer if you know how to do that, maybe someone is serching for it now.
Give this a try. myLoader is the loader that you loaded your swf/swc with the font in its library. myTextField is a pre-existing text field. I set embedFonts to true, but you may not need it.
var MyFont:Class = myLoader.contentLoaderInfo.applicationDomain.getDefinition("FontClassName");
var embeddedFont:Font = new MyFont();
var textFormat:TextFormat = new TextFormat();
textFormat.font = embeddedFont.fontName;
textFormat.size = 24;
myTextField.setTextFormat(textFormat);
myTextField.embedFonts = true;
Maybe you can include a font-fetching class in the swf with the font(s) you want to use. There could be static methods that apply a font to a field, or return the font name given the font's class name. This could help keep the rest of your application cleaner.

AS3: Embedding characters

I having some trouble with TextFields and caracter embedding. As I have understood, the way to embed character in Flash, is to have a TextField in a movieclip that is exported to actionscript via some classname. Then have the TextField embed the characters.
But when i try to use that TextField in my project, I cannot auto resize the field any longer!? Is there a better way to embed charactes? or am I missing some unknow attribute? (and yes i have tried TextField.autoSize = "left" (or "center" or "right")).
The TextField is configured like this in Flash CS4:
Properties:
http://screencast.com/t/0VB6KnNO6G
Library implementation:
http://screencast.com/t/w3yQLqit0veI
And I embed the MovieClip containing the TextField like this:
protected var tabname:MovieClip = new Text(); // The property on the object
Adding the text and setting its Settings:
var txt:TextField = tabname.txt;
if( !contains(tabname) )
{
addChild(tabname);
var format:TextFormat = new TextFormat();
format.bold = true;
format.font = "Arial";
format.size = 12;
format.align = "left";
var dropShadow = new DropShadowFilter(0);
dropShadow.color = 0xFFFFFF;
dropShadow.strength = 2;
dropShadow.blurX = dropShadow.blurY = 5;
dropShadow.alpha = .7;
txt.type = TextFieldType.DYNAMIC;
txt.multiline = tabname.wordWrap = false;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.defaultTextFormat = format;
txt.filters = [dropShadow];
txt.mouseEnabled = false;
txt.x = 10;
}
txt.text = value;
txt.y = Math.ceil((tabmask.height - txt.height) /2);
To embed fonts, don't rely on wrapping them in MovieClips in the library. They should be embedded correctly as Fonts. I have included some basic steps below for embedding fonts, then an example for your particular situation:
1 - Make the textfield Dynamic and click the Embed.. button
2 - Name the font with something meaningful (like the fonts name) and tick the character sets you will be using (usually I select caps, lowercase, numbers and punctuation). Also note the Style is 'Bold', you will need to embed a font set for each style. So if you want to use Bold and Regular, you need to embed 2 fonts.
3 - If you plan on adding textfields dynamically through ActionScript, goto the ActionScript tab and add a class for it (again, use a meaningful name)
4 - Finally click ok, and away you go. I have setup an example, using these steps, and the auto size method, you can see the results below
In Flash, you can click the [Embed...] button below the TextField's character properties. In the window that you get then, you can specify which characters you want embedded in your textfield.
There's a lot more to say about font embedding but this is the simple story. Flash CS5 added TLF TextFields but I don't think you were referring to those, right?
The autoSize property really has nothing to do with font embedding but I guess your TextField is not Dynamic when you cannot auto resize it?
Are you using CS5 or CS4 or earlier by the way?

TextField() sometimes stays BOLDING even when HTML style was reset with setTextFormat()

Any ideas why TextField() sometimes keeps BOLDING (even when the format was reset with setTextFormat()) like this?:
myFormat.bold=false;
defaultTextFormat =myFormat;
defaultTextFormat.bold = false;
setTextFormat(myFormat);
htmlText("This text is bold even when it should not be.")
PS: TextField is editable and the problem occurs when it is edited by user.
UPDATE: I want to get rid of all formatting to avoid the unwanted BOLD. TextField has a bug that when a bolded word is backspaced over by the user, everything in TextField is bolded forever.
I believe that with the TextField control you need to set the TextFeild.styleSheet to change the HTML style. Try something like this:
var myText:TextField = new TextField();
var style:StyleSheet = new StyleSheet();
var fonts:String = "Helvetica Neue, Arial, _sans";
style = new StyleSheet();
style.setStyle(".body", {fontFamily:fonts, fontSize:"12", fontWeight:"normal", color:"#222222", textDecoration:"none"});
myText.styleSheet = style;
myText.htmlText = "this is bold even when should not";