Link to home
Start Free TrialLog in
Avatar of arasburn
arasburnFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I create a rounded-corner textField component in AS3

Hi,

I need to create labels with coloured round-cornered backgrounds.

I am creating a flash file that will be used as a base or template flash file by three designers (including myself) to create hundreds of informative SWF files.

These labels will vary in size (depending on the amount of text) but the font size and position of the text (the padding effectively) must always be consistent.

I found a good article here: http://stackoverflow.com/questions/1223606/as3-rounded-text-field

Unfortunately I am an OKish AS2 coder, who struggles with classes and am only recently migrating uncomfortably to AS3.

After looking online I worked out how to get the above code into a component but my knowledge is not sufficient to do much more with it.

What I want:
- AS3 component (hereby referred to as label component)  that I can drag from library or component panel into a scene
- label component to have a text entry box in the componenrt inspector
- label component to show in the scene
- text to appear in label component

Of the above, I have sort of got the first bit working:
I saved the package as TextBorder.as in a new folder, created an AS3 flash document called demo.fla.
In demo.fla I created a movieclip and then I clicked 'Component Definition' in library and associated the class with TextBorder.as
Also in library I clicked on properties and associated class with TextBorder.as

When I drag my moviclip into the scene and compile it I get a round corner box with a text field saying Hello World. Which I expected.

The instance of the component is invisible on the stage and only appears when you compile/ at runtime.

Additionally I can't alter the hello world text and I can't define anything in the component inspector.

---

My gap (many gaps) in knowledge is how do I take this package and create it into a component that I can see in my development environment (the stage). I have seen a bit about live preview but it asks for a swf and I can't provide one as the label is produced dynamically.

I tried to add a stylesheet element so I could control the text but it is not picking it up, again i am a real newbie at all of this and AS3 so probably getting it terribly wrong.

---

If component is not my best approach, anybody able to suggest a solid work around with moviclips with 9 point scaling? I can't see it working as elegantly as a component, especially as I would have to separate the text layer from the background to prevent the text from scaling etc.

---

Any help or examples please would be amazing thank you.

Kind regards,
Ady
/*code taken from http://stackoverflow.com/questions/1223606/as3-rounded-text-field

modified by myself to include a stylesheet (which doesn't work) and to add padding - which sort of does.
*/

package
{

import flash.display.Graphics;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
//Ady added---
import flash.text.Stylesheet;
//--- Ady end

public class TextBorder extends Sprite
{
    private static const CORNER_RADIUS:int = 5;
    // display objects
    private var background:Sprite;
    private var field:TextField;

    // properties
    private var _text:String;
    //Ady add---
    private var newStyleSheet:StyleSheet = new StyleSheet();
    //--- Ady end

    public function TextBorder()
    {
        background = new Sprite;
        field = new TextField;
        field.autoSize = TextFieldAutoSize.LEFT;

        //Ady added---
        var styleObj:Object = new Object();
        styleObj.fontWeight="bold";
        styleObj.color="#666666";
        newStyle.setStyle(".defStyle",styleObj)

        field.styleSheet = newStyle;
        field.htmlText="";
        //--- Ady end

        addChild(background);
        addChild(field);

        // TESTING:
        text = "Hello World";
    }

    public function set text(newText:String):void
    {
        _text = newText;
        display();
    }

    public function get text():String
    {
        return _text;
    }

    private function display():void
    {
        field.text = _text;

        //Ady add--
        var expX:Number = field.width+20;
        var expY:Number = field.height+20;
        //--- Ady end

        var g:Graphics = background.graphics;
        g.clear();
        //Ady add
        g.lineStyle(0.1,0x000000,0.5,true,"normal");
        //--- Ady end
        g.lineStyle(0, 0x0);
        g.beginFill(0xFFFFFF);
        //--- Ady end
        g.drawRoundRect(-10, -10, expX, expY, CORNER_RADIUS);
        //--- Ady end

        /* originally was this:
        g.drawRoundRect(0, 0, field.width, field.height, CORNER_RADIUS);
        */
    }
}

}

Open in new window

Avatar of deepanjandas
deepanjandas
Flag of India image

Create a CustomTextField which should extend Sprite.

Now have a textFiled within and set all setters and getters in the class to set the properties of the textfield.

Have a base sprite where you draw the border using drawRoundedRect.

Hope this makes sense.

Warm Regards
Deepanjan Das
Avatar of arasburn

ASKER

Hi Deepanjandas,

I'm really sorry but I don't understand.

I am really new to classes, I used them a little in the past in AS2 but even then I think I was using them incorrectly or at least not how they were designed to be used. I really am trying to up my game and use them properly. I have never made a component before either so this is doubly confusing to me.

Would you mind leading me through your answer a little more please?

Do I make a new .as file called customTextField for example? How do I link it in please?

I'm so sorry I'm thick about all of this. I mainly have only ever done procedural type coding before. Objects and classes confuse the heck out of me.
Okie, I will try to create a sample file for you shortly.

Warm Regards
Deepanjan Das
Deepanjandas, that would be amazing of you thank you so much!
ASKER CERTIFIED SOLUTION
Avatar of deepanjandas
deepanjandas
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Deepanjan,

I can only echo what I said earlier, thank you so much for taking the time to do this.

I'm going to type in your code and see what happens. Please may I call on you again if I get stuck, I'm still a little unclear about this.

Cheers,
Ady
Hi Deepanjan,

I have created a new folder (called label). Inside this folder I have created a folder structure of com\dd\controls\core.

I have created 2 .as files one called Component.as and one called CustomTextField. Both these .as files are in the core folder.

In the top level folder (label) I have created an AS3 flash file unorriginally called demo.fla.

I have written in the first frame of demo.fla:
import com.dd.*;

I'm embarrassed to admit at this point I'm now totally stuck.

I have read your code and the minutiae of each bit pretty much makes sense to me, but lloking at it as the two separate chunks of code is baffling me.

I don't know if I am meant to create a dynamic text field and turn it into a component as I had done in my own earlier attempts or if I need to make a movieClip or whatever.

Please would you mind guiding me through this bit? I am so desparate to understand it but it feels too big a project to cut my teeth on. In hindsight I should start on something much smaller, but I do need to achieve the result I set out to do and I believe components are my best approach to do this.

I clearly need to learn more about AS3 and classes etc.

If you don't have time to help then I totally understand, you have already done loads and definitely will get full points.

Thank you again,
Ady
Deepanjan has been amazing and has given me some amazing code, unfortunately I don't fully know how to use it. I am 100% confident it will work. I just need to work out how.
Okie, here you go:
The package structure will be like this:

- projectDir
        |__ com
        |        |__ dd
        |                |__ controls
        |                           |__ CustomTextField.as
        |                           |__ core
        |                                    |__ Component.as
        |__ demo.fla

Now in demo.fla place this code:
//imports
import com.dd.*;

var myTextField:CustomTextField = new CustomTextField();
myTextField.cornerRadius = 16;
myTextField.text = "Hello World!"
myTextField.setSize( 200, 30 );
addChild( myTextField );

Open in new window


Remember, you might need to tweak or update the CustomTextField class to as per your requirements.

Warm Regards
Deepanjan Das
Hi Deepanjan,

Firstly thank you so much for your last post. I had set the folder structure up as indicated, although I had originally placed the cutomtextfield.as field in the core folder.

I have everything set up as you described above, and I placed the code in that you listed.

I get the following errors:
1046: Type was not found or was not a compile-time constant: CustomTextField.
1180: Call to a possibly undefined method CustomTextField.

I know this is a huge imposition, but please can you advise me what I am doing wrong. I am very aware that you are effectively doing this all for me, my coding skill does not cover this area and as such I feel very out of my depth.

I have just enrolled in an Open University to study programming, so in time I am confident that I would be able to do all of this sort of thing on my own, but right now - I am struggling massively.

I just can't see how this will become a component. I'm happy to raise this as a related question or post a new question to assign you more points.

Kind regards and thank you again for taking the time and effort to help me
@arasburn, it is my pleasure in providing solutions, so points does not always matter.
I believe that I also learn while providing solutions.

Anyways, coming back to the problem.

May be add this as import:

import com.dd.controls.CustomTextField;

Let me know.

Warm Regards
Deepanjan Das
Hi Deepanjan,

Again thank you for your continued support. I tried the above line, no joy.

At this stage I thought it might be wise for me to upload my file in case you wouldn't mind having a look at it please.

Zip file contains the demo.fla and the folder structure as outlined in your previous comment and the subsequent AS files.

Thank you again,
Ady label.zip
Here you go:demo.zip

Warm Regards
Deepanjan Das
Hi Deepanjan,

Thank you for taking the time to look at my files again. I downloaded your demo.zip and tried to compile it. I experienced the following error:
CustomTextField.as line 10  1172: Definition flashx.textLayout.formats:TextAlign could not be found.
Hi Deepanjan,

I wrote you a massive reply and it got truncated. Sorry the above comment appeared so short.

More to follow; I wrote this 3 times now and it has not uploaded it... will do it in chunks.

Cheers,
Ady
Hi Deepanjan,

As mentioned above I experienced a problem with line 10 of the CustomTextField.as file, the flashx import, and commented it out - after doing so, the file compiled fine.

Ideally is there a way to include the flashx line please? From looking at your code I can see it is important as it helps define the look of the label.

Once again Deepanjan, I just want to say how much I appreciate all you have done. It is really cheeky of me to ask this, but please can you help some more? In my original post I was trying to create a component, and whilst I understand that is what you have produced for me, I was hoping / aiming to have it in a more designer friendly state. There are three of us on the project, and I laughably have the most programming experience, my colleagues have zero. I was hoping to have a component that could be pulled out of the component library and used with the component inspector, ideally with a livePreview so they/we could properly layout these labels within the scene. If you could make that final leap and show me how you take all the code you have done so far and turn it into such a component I would really appreciate it; as the code stands, I'm confident my colleagues will unfortunately not be able to use it.

As a small aside, I noticed in your bio that you have created some commercial components for flashcomponents.net, I strongly suggest you consider turning the work you have kindly done for me into such a component, I am confident that people would want to buy it. Before I came onto experts exchange with the naive belief I could make my own component I had scoured the net looking for a component I could buy and couldn't find one! Anyway, is just a thought :)

Kind regards Deepanjan, and again, thank you so much!
Ady
Thanks Ady for your suggestion :) I will keep this in mind for sure.

Just remove that import, not sure how that came along.
Rest will work.


Warm Regards
Deepanjan Das