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

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.

Related

How to convert different line of a dynamic text box to Movie clip in AS3

I have a question regarding to my project which is How to convert different line of a dynamic text box to Movie clip in AS3?
Actually, I have an text file named test.txt. For instance:
It consists of:
today is Sun;
today is Mon;
today is Tue;
today is Wed;
today is Thu;
today is Fri;
today is Sat;
and then I want to put all of them into an array and then a string to show them in the dynamic text Box called text_txt.
I have different objects in my library. If we see "sun" in the first line, the first object (obj01) will be shown in a specific area which is inside a movie clip called mc.
The question is here:
Fist: if I have different texts in my first line. for instance "today is sun;". how to find that the "sun" is in this line???
Second: How to convert different line of a dynamic text box to Movie clip. So, if user click on the "obj01", the first line in the dynamic textbox will become bigger???
Thanks for your time and help in advance.
import flash.events.MouseEvent;
var flag:int = 0;
//load the txt file
var myTextLoader:URLLoader = new URLLoader();
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.load(new URLRequest("test.txt"));
//when the scene loads, all the info from txt file shown into the dynamic text;
function onLoaded(e:Event):void
{
//put all the info into array
var days:Array = e.target.data.split(/\n/);
var str:String;
//show them in the dynamic text
for (var i=0; i<days.length; i++)
{
str = days[i];
text_txt.appendText(str + "\n");
//if one text founded, do somethind and shoe an objectinthe output;
switch (str)
{
case "sun;\r" :
var obj01:Show = new Show();
mc.addChild(obj01);
obj01.x = -200;
obj01.y = -15;
break;
default :
trace("None of the above were met");
}
}
obj01.buttonMode = true;
//if the object clicked then something must be happend to the first line in the dynamic text box
obj01.addEventListener(MouseEvent.CLICK, firstLine);
function firstLine(e:MouseEvent):void
{
flag = 1;
switch (flag)
{
case 1 :
trace("Clicked!");
//the first line in the text box should get a different background and become more bigger
//Do I need to convert the first line to a movieclip? OR I need to do in another way?!
break;
default :
trace("None");
}
}
}
First: if I have different texts in my first line. for instance "today
is sun;". how to find that the "sun" is in this line???
split whole of the dynamic-text value with its lines to an Array
then check each array item if contains that key ("sun").
you have to use some thing like it Array[x].indexOf(key) >= 0
Second: How to convert different line of a dynamic text box to Movie
clip. So, is user click on the "mc.obj01", the first line in the
dynamic textbox will become bigger???
i just give you an example to find the reason of difference in size of movieclip and text-display. this image comes from adobe-flash. a text field with its value "brown fox jumps" has the same size as the green rectangle. but the text-size shown by the red rectangle.
Green is textfield size, same as textField.width & textField.height
Red is text size, same as textField.textWidth & textField.textHeight
so you must resize your movieclip/textfield to second Values(Red);
my answer is not walkthrough but a guidance.
After too much research and try a lot of functions, I have find the answer for Second part of this question.
I was sure that there must be a way to do it and that's why I made it as my Favorite question.
//
var newMcTitle:Array = [];
//put all the info into array
var days:Array = e.target.data.split(/\n/);
for (var i=0; i<days.length; i++)
{
str = days[i];
//put the texts line by line into a textfield and put the textfield into a movie clip
var newMc:MovieClip = new MovieClip();
var newText:TextField = new TextField();
newText.text = days[i];
newMc.addChild(newText);
newMc.x = 30;
newMc.y = positionY;
newMc.scaleX = 2;
newMc.scaleY = 2;
addChild(newMc);
positionY += 30;
//unique instance names:
newMc.name = days[i];// or more generally when no arrays used
newMc.name = 'mc_' + i;
//trace(newMc.name);
//asssign unique property to be used to identify newMc
newMc.ivar = i;
//populate an array instantiated outside for-loop (eg, var newMcA:Array=[];)
newMcTitle.push(newMc);
}
And now you can use:
//if the object clicked then something must be happend to the first line in the dynamic text box
obj01.addEventListener(MouseEvent.CLICK, firstLine);
function firstLine(e:MouseEvent):void
{
flag01 += 1;
switch (flag01)
{
case 1 :
//the first line in the text box should get a different background and become more bigger
newMcTitle[0].scaleX *= 1.2;
newMcTitle[0].scaleY *= 1.2;
break;
default :
trace("None");
}
}

AS3 - Add scroll bar to dynamically generated text field?

I'm new to actionscript 3, so please forgive me.
I am loading a text file into my flash file and then dynamically creating a text field which will take in the myBody text from that file. The problem is that the text in myBody could be very long, so I would like to add a scroll bar when needed to the dynamically generated text field. Is there any way to this?
var myTextLoader:URLLoader = new URLLoader();
myTextLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
var myTextField_txt:TextField = new TextField();
myTextField_txt.multiline = true;
myTextField_txt.wordWrap = true;
myTextField_txt.border = true;
myTextField_txt.height = 100;
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
myTextField_txt.text = e.target.data.myBody;
addChild(myTextField_txt);
}
myTextLoader.load(new URLRequest("mySampleFile.txt"));

AS3 - setTextFormat(), defaultTextFormat not working as designed?

UPDATED with simpler snippet
I am building a Flash swf to check through some XML files using AS3.
I only have access to the Flex compiler, not the actual Flash IDE or Flashdevelop, so all my xml checking code is a single AS file, compiled into an swf. In order to display information, I create and add a TextField and call appendText() (I use += in this snippet).
The snippet below seems to not set the new format on existing text. It appears to not have any effect whatsoever on the text.
I was under the impression that setTextFormat would change the existing text and that any new text added after calling setTextFormat would use the defaultTextFormat object. This does not appear to be the case in my code. Am I incorrect in my understanding?
public function AS3Tester()
{
display_txt = new TextField();
display_txt.multiline = true;
display_txt.width = 1024;
display_txt.height = 768;
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.font = "Lucida Console";
display_txt.defaultTextFormat = tf;
display_txt.text = "Text Before setTextFormat\n"; //Should use default style tf
addChild(display_txt);
var tf2:TextFormat = new TextFormat();
tf2.size = 18;
tf2.color = 0xFF0000;
display_txt.setTextFormat(tf2);
display_txt.text += "Text after setTextFormat\n"; //previous line should use tf2, this new line should use default tf
}

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

Set text outlining / border in Actionscript 3.0

How can I set the properties for the text outline / border for each character in a line of text in AS3 ?
I don't think you can. What you can do is use a blur filter to mimic the appearance of an outline. Just paste this into an empty AS3 movie:
var txt:TextField = new TextField();
this.addChild(txt);
txt.appendText('Lorem ipsum');
txt.autoSize = TextFieldAutoSize.LEFT;
txt.antiAliasType = flash.text.AntiAliasType.NORMAL;
txt.selectable = false;
var txtFormat:TextFormat = new TextFormat();
txtFormat.size = 40;
txtFormat.font = 'Helvetica';
txt.setTextFormat(txtFormat);
txt.defaultTextFormat = txtFormat;
var outline:GlowFilter = new GlowFilter();
outline.blurX = outline.blurY = 1;
outline.color = 0xFF0000;
outline.quality = BitmapFilterQuality.HIGH;
outline.strength = 100;
var filterArray:Array = new Array();
filterArray.push(outline);
txt.filters = filterArray;
Try playing with the strength, blurX, blurY and quality properties, in order to obtain different appearances. I think that's about the closest you can get to a text outline.
PS: font embedding would greatly improve the quality of the effect, as well as making the antialias work properly.
i am not shore i understand but you can use same kind of
filter on the testbox and by doing so you can get a same kind of a border
in each one of your letters