Reputation: 2693
I need to set a textfield to _sans if a certain language is used. Usually it uses an embeded font. How can I set it to _sans through AS3?
Thanks. Uli
Upvotes: 0
Views: 4603
Reputation: 4260
To use the system _sans font use the following code:
var tf : TextField = new TextField(); // create a text field
var fmt : TextFormat = new TextFormat(); // create a text format
with(fmt) {
font = "_sans"; // using sans results in the use of arial on windows and helvetica on MacOS
size = 12;
color = 0xFFFFFF; // white
}
with (tf) {
embedFonts = false; // if you skip this you'll get Times (at least in Windows)
defaultTextFormat = fmt;
}
Not embedding fonts results in smaller swf's >> shorter download time, less traffic. Especially if you want to be able to use different character sets (russian, greek, chinese) embedding can result in a big swf file size.
Upvotes: 0
Reputation: 3851
Using system.Capabilities to detect the systems language setting might not be the best route to take. e.g. you might have an English speaking person working in India who wants to use your file.
You might want to build in some kind of language choosing option.
In this example I set a variable (alphabetToUse) whether the chosen language is latin or not.
var myFont:Font;
var alphabetToUse:String;
myFont = new DinReg();
//for latin
var myFormat:TextFormat = new TextFormat();
myFormat.size = 14;
myFormat.align = TextFormatAlign.LEFT;
myFormat.font = myFont.fontName;
//for anything else
var myDefaultFormat:TextFormat = new TextFormat();
myDefaultFormat.size = 14;
myDefaultFormat.align = TextFormatAlign.LEFT;
myDefaultFormat.font = "_sans";
var myText:TextField = new TextField();
myText.type = TextFieldType.DYNAMIC;
myText.antiAliasType = AntiAliasType.ADVANCED;
myText.textColor = 0xFFFFFF;
myText.multiline = true;
if(alphabetToUse == "latin") {
myText.embedFonts = true;
myText.defaultTextFormat = myFormat;
} else {
myText.embedFonts = false;
myText.defaultTextFormat = myDefaultFormat;
}
myText.border = false;
myText.wordWrap = true;
myText.width = 345;
myText.height = 28;
myText.x = 0;
myText.y = 0;
addChild(myText);
myText.htmlText = "some text";
Upvotes: 1
Reputation: 11590
To detect the current language of the client you can use:
flash.system.Capabilities.language; //in my case returns "en"
To dynamically load a font at run time there are some good tutorials out there, including this one: http://bryanlangdon.com/blog/2007/03/22/loading-fonts-dynamically-in-actionscript-2-and-3/
I hope that helps.
Edit
To dynamically change fonts that are already embedded, first turn on font embedding for your individual text fields: (untested code)
myTextField_txt.embedFonts = true;
Then create and set the text format of the textFields you want changed. Something like:
var myformat:TextFormat = new TextFormat();
myformat.font = "_sans";
myTextField_txt.setTextFormat(myformat);
Optionally you can set additional things in the TextFormat, such as size, color, etc..
Upvotes: 1