ADOBE/ ActionScript

[AS] 플래시 액션 스크립트 as3 TextField에 이미지 넣기 관련

AlrepondTech 2020. 9. 22. 00:57
반응형

 

 

 

 

=================================

=================================

=================================

 

 

 

 

출처: http://stackoverflow.com/questions/12068933/using-symbol-from-library-in-htmltext-img-tag-in-actionscript-3

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using symbol from library in htmlText <img> tag in ActionScript 3

Problem is this: I need to add image to textField using img tag, but I cannot reference a symbol from a library in my swf file.
AS linkage for this symbol is symbol1 and i tried embedding this swf in my class but it always gives error #2035 - URL not found
Adobe says that img tag accepts a library symbol, but I couldn't find any example where this is true.
Any help would be appreciated.
actionscript-3 flash img htmltext
 

2 Answers

It's a while since I did this but I think you need to create a movie clip in the library with the bitmap inside it, then export that for ActionScript, then add that as the linkage in the tag.

So, if your movieClip is exported as 'myImage_mc", your html will be:

<img src="myImage_mc" width="100" height ="100"/>

Update.

To clarify, here's my symbol in the library:

here's my actionscript:

import flash.text.TextField;

var textField:TextField = new TextField();
textField.htmlText = "<p>HKP</p><img src='HKP'/>";
textField.x = textField.y = 100;
stage.addChild(textField);

And here's the result (which admittedly needs a bit of tweaking):

Note: this doesn't seem to work if the img is the only tag in the field. You have to add some text, even if it is not visible. An empty P won't work, so either of these will fail:

textField.htmlText = "<img src='HKP'/>";
textField.htmlText = "<p></p><img src='HKP'/>";

.. but this works:

textField.htmlText = "<p> </p><img src='HKP'/>";

.. which is pretty much a classic Adobe gotcha ;)

 
It works fine for me in CS6. I'll update my answer a little. –  JcFx Aug 22 '12 at 9:57
Thank you for this example, but maybe I wasn't clear enough. I'm using FlashBuilder and i need to change htmlText in TextField in runtime, to add images into text, to replace some tag with an image. It works for me in Flash Professional too, but I'm having trouble doing this in FlashBuilder. Thanks again for your effort. – puzdrow Aug 22 '12 at 10:17
You didn't mention FlashBuilder. It should work just the same way if you create the symbol in FlashProfessional, export that whole FLA as a SWC, then include the SWC in your project as a library. You can google this - there are tons of references. I'm sure this is possible but don't have any more time this morning, so good luck, I hope you work it out. –  JcFx Aug 22 '12 at 10:21 

for your source try adding the image extension, such as image.jpg or image.png

textfield.htmlText = "<img src='image_name.jpg' width='100' height='100'/>";

 

 

 

반응형

 

728x90

 

 

 

=================================

=================================

=================================

 

 

 

출처: http://stackoverflow.com/questions/13326585/how-do-i-add-a-background-image-to-a-textfield-in-as3

how do I add a background image to a textfield in as3

Hey guys I am developing an app for IPhone and I am trying to figure out how to add a background image to a textfield that is populated by phone numbers a user stores.

My function -

function configureLabel():void
{
    label = new TextField();
    label.autoSize = TextFieldAutoSize.LEFT;
    label.background = true;
    label.border = true;
    label.width = 50;
    label.x = 55;
    label.y = 80;
    //label.htmlText = "whasfd";
    var format:TextFormat = new TextFormat();
    format.font = "arial";
    format.color = 0xFF0000;
    format.size = 10;
    format.underline = false;
    //format.leftMargin = 50;


    label.defaultTextFormat = format;
    addChild(label);
}

 

2 Answers

activeoldestvotes

You could make a display object class that contains the textfield and the background. Then you simply add an event listener to the textfield that listens for any changes. Every time there is a change the background would resize itself to the textfield's current size.

Here is an example:

package 
{
    import flash.display.Bitmap;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;

    public class Main extends Sprite 
    {

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
            loader.load(new URLRequest("images/koala.jpg"));

        }// end function

        private function onComplete(e:Event):void {

            var bitmap:Bitmap = ((e.target as LoaderInfo).content as Bitmap);

            var container:TextFieldContainer = new TextFieldContainer();
            container.textField.autoSize = TextFieldAutoSize.LEFT;
            container.textField.defaultTextFormat = new TextFormat(null, null, 0xFF0000);
            container.textField.type = TextFieldType.INPUT;
            container.textField.multiline = true;
            container.setBackground(bitmap);
            addChild(container);

        }// end function

    }// end class

}// end package

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;

class TextFieldContainer extends Sprite {

    private var _textField:TextField;
    private var _backgroundContainer:Sprite;

    public function get textField():TextField {

        return _textField;

    }// end function

    public function TextFieldContainer():void {

        _backgroundContainer = new Sprite();
        addChild(_backgroundContainer);

        _textField = new TextField();
        _textField.addEventListener(Event.CHANGE, onChange);
        addChild(_textField);

    }// end function

    private function onChange(e:Event):void {

        resizeBackground();

    }// end function

    public function setBackground(displayObject:DisplayObject):void {

        if (_backgroundContainer.numChildren > 0) {
            _backgroundContainer.removeChildAt(0);
        }// end if

        _backgroundContainer.addChild(displayObject);

        resizeBackground();

    }// end function

        private function resizeBackground():void {
            _backgroundContainer.width = _textField.width;
            _backgroundContainer.height = _textField.height;
        }

}// end function

 

=================================

=================================

=================================

 

 

 

반응형