AS3 TLF text bolding as html - html

I am having a problem with TLF text in flash.
So I'm reading a .csv file. In it I have "Some text<br><b>some bold text</b><br>bla bla"
I read this in and output it to a textbox that is set to standard Arial.
The <br> tags work fine but the <b> tags do NOT... Any suggestions?
function loadText(fileToLoad:String):void
{
csvLoader = new URLLoader();
csvLoader.dataFormat = URLLoaderDataFormat.TEXT;
csvLoader.load(new URLRequest("TextFiles/" + fileToLoad + ".csv"));
csvLoader.addEventListener(Event.COMPLETE,CSVLoaded);
}
function CSVLoaded(e:Event):void
{
var str:String = csvLoader.data as String;
var arr:Array = str.split("\n");
for (var i:int=0; i<arr.length; i++)
{
arr[i] = arr[i].split(';');
}
ExtraInfo.Heading.htmlText = arr[0][0];
ExtraInfo.MainText.htmlText = arr[0][1];
}

I tried with just <b></b> tags... didn't work for me either.
Then it turns out to be Font embedding issue.
Since you are using Arial, you can set the anti-alias property of the TLF textfield to use device fonts.
But it's always better to embed the fonts...

Related

How do I format different lines of text differently, from a textfield created in AS3, with text populated from a txt file?

So I have a flash file in AS3, latest version of flash.
It creates a text box in AS3. It then uses AS3 to grab text from a text file (2 lines) and loads it in. I then used further code to format the text size, font, color etc.
But NOW...I need line 1 of the text box to be a certain format (large, caps) and the second line to be a different format (smaller, no caps)
Here is all my code below:
//BEGIN TXT LOADER
var myTextLoader:URLLoader = new URLLoader();
var winnerText:TextField = new TextField();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
winnerText.text = e.target.data;
addChild(winnerText);
}
myTextLoader.load(new URLRequest("EditableText.txt"));
//BEGIN TEXT BOX FORMATTING
winnerText.width = 1920;
winnerText.height = 300;
winnerText.y = 430;
//BEGIN TEXT & FONT FORMATTING
var casinoBranding:TextFormat = new TextFormat();
casinoBranding.size = 90;
casinoBranding.align = TextFormatAlign.CENTER;
casinoBranding.font = "Bliss Pro";
casinoBranding.leading = -50;
winnerText.defaultTextFormat = casinoBranding;
You can apply a TextFormat like #Aaron suggests. Another way of doing is to use stylesheets. Here is an example
http://snipplr.com/view/39474/as3-textfield-and-stylesheet-example-created-in-actionscript/
You can apply a TextFormat to a specific range of text using TextField/setTextFormat().
To apply a different text format to the first line of text you can do this:
var casinoBranding:TextFormat = new TextFormat();
var casinoBrandingFirstLine:TextFormat = new TextFormat();
// ... apply formatting options
function onLoaded(e:Event):void {
winnerText.defaultTextFormat = casinoBranding;
winnerText.text = e.target.data;
winnerText.setTextFormat(casinoBrandingFirstLine, 0, winnerText.getLineOffset(1));
}
Note that if word wrapping is involved it changes what the "first line" really means.

Actionscript 3 Text below image in TextArea.htmlText

I would like to display an image and a text below it in a TextArea but I can't figure out why the text is displayed on the right of the image.
Here is my code :
var textArea:TextArea = new TextArea();
var urlLdr:URLLoader = new URLLoader();
urlLdr.addEventListener(Event.COMPLETE, completeHandler);
urlLdr.dataFormat = URLLoaderDataFormat.TEXT;
urlLdr.load(new URLRequest("myText.html"));//the file only contains the string "Test image" with no html tag
function completeHandler(event:Event):void {
var str:String = event.target.data as String;
htmlTextArea.htmlText = "<p><img src='myImage.jpg'/> </p>";
htmlTextArea.htmlText += "<p>"+str+"</p>";
addChild(htmlTextArea);
}
And I get :
I tried by adding the tag < br/ > , and \n in the htmlText between the image and the text but it only makes the text be written lower on the right.
With :
htmlTextArea.htmlText = "<p><img src='myImage.jpg'/> </p><br/><br/><br/>";
I get :
So, how to write the text below the image?

Flash CS5, dynamic font embeding problem

I'm trying to create dynamic textfield with font embedding. Embeding is dynamic like this:
public class TextFormats extends TextFormat {
private var TF:TextFormat = new TextFormat();
[Embed(source = "/fonts/tahoma.ttf", fontWeight = "normal", fontFamily = "tahomaNormal")]
var fontTahoma:Class;
private var fTahoma:Font;
public function TextFormats():void {
fTahoma = new fontTahoma();
}
public function format(fmb:String):TextFormat {
TF.letterSpacing = -1;
TF.font = fTahoma.fontName;
switch(fmb) {
case "combolist_label":
TF.color = 0x383838;
TF.size = 13;
TF.letterSpacing = 0;
break;
}
return TF;
}
}
When I compile it in flash CS4, embeded text appears on stage fine! But, when I tried to compile it with flash CS5, the text do not appear and no error warnings.
What is the reason? Should I use another methods for font embeding?!
There are a few articles published about the big difference in font embedding that is new in CS5. I think this one is quite good:
Having trouble with embedded fonts and HTML text in Flash CS5?
The manner in which fonts are embedded is an improvement in CS5 -- but it means that all your CS5 dynamic text fields break when you open the FLA for editing in CS5! Which sucks! (Everything still works fine as deployed in SWFs.)
If you open the CS4 FLA in CS5, you basically need to rebuild the dynamic text fields and reapply the embedding.
There is code here:
import flash.text.*;
var font:Font1=new Font1();
var txt_fmt:TextFormat=new TextFormat();
txt_fmt.font=font.fontName;
txt_fmt.size=24
var txt:TextField=new TextField();
txt.autoSize=TextFieldAutoSize.LEFT;
txt.defaultTextFormat=txt_fmt;
txt.embedFonts=true
txt.text="Designscripting.com"
txt.selectable=false
addChild(txt);

Rendering text in AS3

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.
Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.
You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.
A possible example.
var myField:TextField = new TextField();
myField.text = "my text";
myField.x = 200;
myField.y = 200;
addChild(myField); // assuming you are in a container class
If TextField doesn't work, you can create text using this method:
var format:ElementFormat = new ElementFormat();
format.fontSize = 26;
format.color = 0x0000FF;
var textElement:TextElement = new TextElement('Hello World!', format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
var textLine:TextLine = textBlock.createTextLine(null, 500);
textLine.x = (stage.stageWidtht - textLine.width) / 2;
textLine.y = (stage.stageHeight - textLine.height) / 2;
addChild(textLine);
Look at:
Creating and displaying text in ActionScript 3.0 Developer’s Guide

Flash AS3, is text anti-aliasing controlled by code?

Is text anti-aliasing controlled by code, or is it embedded? What I want to do is take a swf file that someone else has made, find all of the text in the swf file and change all of the anti-aliasing modes from animation to readability. Does it work that way in as3? I honestly haven't tried any anti-aliasing in code yet.
you can set the antiAliasType of a textfield to 'advanced', which gives you fine control over sharpness and thickness. however, the fonts must be embedded (inclusive: the textfield must have embedFonts set to true, the TextFormat objects must have font properties exactly equal to the fontName of the embedded font, and the fonts must be already compiled). so technically 'is it possible?' - yes. is it likely to work the way you want it to? no, unless you plan on working with a swiff that you know is using embedded fonts already. then you'll need to grab all the textfields from the loaded swiff (you could use something like this: http://upshots.org/?p=107, then use array.filter to get back only TextField objects), then apply your logic.
EDIT: adding code sample
// assuming you're using the DisplayList class linked above
var request:URLRequest = new URLRequest("textfields.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
loader.load(request);
function completeHandler(event:Event):void{
var content:DisplayObjectContainer = event.target.loader.content as DisplayObjectContainer;
addChild(content);
var children:Array = new DisplayList(content);
children = children.filter(function(item:Object, index:int, array:Array):Boolean {
return item is TextField;
});
children.forEach(function(item:Object, index:int, array:Array):void {
var textfield:TextField = item as TextField;
textfield.antiAliasType = AntiAliasType.ADVANCED;
textfield.sharpness = 100;
textfield.thickness = 100;
});
}
just ran a quick test - works as described.