How do I make a text box in ActionScript 3? - actionscript-3

I'm trying to learn ActionScript 3, but right now I'm hung up on getting a basic interface set up. I've found out that newTextField will make one, but I don't know how to put it where I want.
I've checked out some source code for other projects using the same IDE (Adobe Flash CS6) and they seem to have access to a textbox symbol. So how do I make one of the textbox symbols with the IDE, or using some code?

var tf:TextField = new TextField;
tf.text = "Hello World!";
addChild(tf);
These will create black text reading 'Hello World!'.
Find out more about text fields here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html

Related

Why can't I create an instance of flash.text.StaticText?

The documentation says: "You cannot create a StaticText object using ActionScript. Only the authoring tool can create a StaticText object. An attempt to create a new StaticText object generates an ArgumentError."
Why is that? How does Flash Professional create StaticText, and why can't I do the same with AS3?
StaticText is just a DisplayObject, with a readonly text property. It's the Flash Authoring tool that actually creates a graphical element (the text) and draws it onto the stage like a circle or a square.
Just use Textfield instead if you need to add text with AS3.
import flash.text.TextField;
var myText:TextField = new TextField();
myText.text = "Hello World";
addChild(myText);
The fact that you created the text with code, rather than on stage with the authoring tool, means that by definition, it is a dynamic text field, not a static text field.

Create text fields in Flash CS5

Im probably some 4 hours old working with flash CS5, and im trying to create a website that just displays a goggle map with two text fields that the user would enter some details(its for demo purposes), Have no issue with the google Map Part, but i have probably the dumbest Question ever, how do i create a textfield in flash. Or am i using the wrong tool for the task?
Thank You
If you are trying to do so via AS3, it's really simple. Given some parent clip you'd like the textfield to be placed into
var tfTextfieldName:TextField = new TextField();
tfTextfieldName.type = TextFieldType.INPUT;
tfTextfieldName.border = true;
tfTextfieldName.x = 10;
tfTextfieldName.y = 10;
tfTextfieldName.multiline = true;
tfTextfieldName.wordWrap = true;
parentClip.addChild(tfTextfieldName);
I took this straight from the Adobe doc page for the Textfield Class. There you'll all the events/properties available.

AS3: Arabic Embed Fonts

I am using as3 and trying to use embed fonts with Arabic text but it doesn't work
So is there a way that I can access from .tff file the shape of the character or make it works using embed fonts
As using system fonts works but at same time its bad as on some machines it is reverted so I want to use embed font
Hope anyone can help
I found code by some one to change the input arabic text to the correct shape based on the position in text and I modified it to work perfectly with my code :)
here the AS3 code file: http://www.akhalifa.com/testing/ArabicAS3File/ArabicStringUtils.as
and here an example on how to use it: http://www.akhalifa.com/testing/ArabicAS3File/Main.as
You can use any arabic/persian input language like Maryam4
then write your sentence in this program after that copy it and paste in your code
also as #Goran Mottram said embed your font (for this situation if you use Maryam you must select F_fonts)
but noticed that in your code if you paste your arabic language it shows you wired character like ÂMoø , but dont worry if you publish you see a correct arabic word with rtl support
If you have the font installed on your system, make sure to import the font into your library and tick the box "Export for ActionScript" and give it an appropriate name, say MyArabicFont, then bind it to textfield using the following code:
// create the font
var myFont:Font = new MyArabicFont();
// assign to textformat
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = myFont.fontName;
// assign to textfield
var myTextField:TextField = new TextField();
myTextField.defaultTextFormat = myTextFormat;
myTextField.embedFonts = true;
myTextField.text = "Hello, world";
this.addChild(myTextField);

Form elements do not appear in output

I have created a Flex project in Flash Builder 4.5.
Next I added fl.controls libraries (and then mx.controls libraries) in the project.
I am adding a screenshot so you can see the setup and the code.
However when I run/debug it, nothing appears in there. Totally white.
I've worked with fl.controls before, I used Flash CS5 to compile the ActionScript project and they worked correctly.
Is there any particular reason why it does not work in Flash Builder?
UPDATE: When I add graphics to the text input, i.e.
ti.graphics.beginFill(0xFF0000);
ti.graphics.drawRect(0, 0, 100, 30);
ti.graphics.endFill();
I do see a red rectangle shape. But still no editable text input box. I tried setting ti.editable = true but no use.
Try this:
Create a new FLA in Flash Professional. Add the TextInput component to your library. Notice that not only does the TextInput class get added to the library, but also a folder of "Component Assets" - skins and such. Flash Professional components are not just code - they are code and graphics.
If you want to use fl.controls.TextInput in Flash Builder, you can publish that FLA you just created with the "export swc" option checked. Include that swc in your Flash Builder project, and you'll be able to instantiate the TextInput in your code. If you want to add other Flash components, add them to the library in the FLA and republish the swc.
I would prefer using Flex itself, it will speed up your development. It's simple and does a lot of work for you.
If you do not want to do this you should give your text some properties like width, height, textFormat or use a css document for the text format. You could also give your text a border ( it should be a property of the TextInput ).
Do not forget to set the text format, otherwise the TextField doesn't know which font to use.
Sample for TextInput ( Bottom of Page )
http://help.adobe.com/de_DE/FlashPlatform/reference/actionscript/3/fl/controls/TextInput.html
Sample for Flex
http://help.adobe.com/de_DE/FlashPlatform/reference/actionscript/3/spark/components/RichEditableText.html
You can try this code:
private var ti:TextField = new TextField();
public function FormText(){
//adds ti possibly underneath 'this': stage.addChild(ti);
//adds ti on top of 'this':
stage.addChildAt(ti,getChildIndex(this));
// makes the TextField editable:
ti.type = TextFieldType.INPUT;
}
This should not only make sure that the TextField exists when the class is instantiated, but also puts the form in front of the class.
But this also assumes that the class has been added to the stage; so it may be better to add it like this:
private var ti:TextField = new TextField();
public function FormText(){
addEventListener(Event.ADDED_TO_STAGE, addedHnd);
}
private function addedHnd(e:Event)
{
removeEventListener(Event.ADDED_TO_STAGE, addedHnd);
//adds ti possibly underneath 'this': stage.addChild(ti);
//adds ti on top of 'this':
stage.addChildAt(ti,getChildIndex(this));
// makes the TextField editable:
ti.type = TextFieldType.INPUT;
}
Please review the TextField docs, too.
Why do you add your control to the stage? You should just add it to your form. i.e. this.addChild(ti)

How to add RichEditableText of TextArea using only ActionScript

My head is spinning from two days of trying to find an answer to this seemingly simple question.
I'm developing a Flex/AIR application built entirely in ActionScript -- there's no MXML beyond what was originally auto-created.
I need to dynamically generate some kind of editable text-field with high control over formatting. The TLF text fields all seem great, except that I can't get any of them to render on the screen. Due to the nature of the application, they have to be inside a MovieClip, but since I've read that everything must be a descendant of UIComponent, I use UIMovieClip, which is AddChild'ed to the stage.
I'm about to go crazy here, the whole application is in jeopardy over this. I CAN NOT use MXML, and all the 10,000 examples on the internet are MXML. I need to generate these dynamically. I need to generate upwards of 50 fields under one movieclip based on database data. There's no way to hardcode that with MXML. Please don't suggest to change this. The GUI is very specific about this, and it's the right GUI.
In two days of searching, I can't find a single example in ActionScript, only MXML. I've tried everything that smelled like an example.
Is there some obvious general pointer I'm missing? I'll be happy to post code, but it doesn't make sense because I've been through so many examples.
Does anyone have the simplest possible code for creating any kind of TLF text editing field in ActionScript only (zero MXML), which is then added to a MovieClip or UIMovieClip, which is added to the stage of a desktop AIR application?
I will greatly cherish any help here.
Best,
Per
This should get you started:
//create your TextFlow component
var textFlow:TextFlow = new TextFlow();
var p:ParagraphElement = new ParagraphElement();
var span:SpanElement = new SpanElement();
span.text = "hello world";
p.addChild(span);
textFlow.addChild(p);
//create a Sprite that will contain the text
var textBlock:Sprite = new Sprite();
//create a controller for compositing
var controller:ContainerController = new ContainerController(textBlock);
//set the size of the composition
controller.setCompositionSize(100, 200);
//make the controller control the TextFlow object
textFlow.flowComposer.addController(controller);
//update the composition
textFlow.flowComposer.updateAllControllers();
//add to the stage
addChild(textBlock);
About the size: it is important you use setCompositionSize() instead of the Sprite's width and height properties.
Using addController() you could spread the text over several Sprites. Each Sprite would have its own ContainerController, but all would share the same FlowComposer which would calculate the composition.
warning : using TLF like this can be pretty complicated. Above code is the bare minimum to get things running. I do not know your requirements, but you'll probably hit a few other roadblocks along the way. You have to ask yourself this question: are you really willing to drop all the built-in features of TextArea? It might cost you months of development to get things right, depending on the requirements. You still may want to reconsider your architecture...