=================================
=================================
=================================
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'/>";
=================================
=================================
=================================
출처: 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
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
=================================
=================================
=================================
'ADOBE > ActionScript' 카테고리의 다른 글
[AS] air for iOS에서 swf reload는 지원하지 않는다. 관련 (0) | 2020.09.22 |
---|---|
[AS] 플래시 액션 스크립트 stageWebView 에서 flash와 html간의 데이터 통신방법 관련(pc, 모바일) (0) | 2020.09.22 |
[AS] 액션스크립트 SharedObject 쿠키와 비슷하게 저장 제어 관련 (0) | 2020.09.22 |
[AS] 액션스크립트 쿠키 제어 관련 (0) | 2020.09.21 |
[AS] 플래시 액션스크립트 AS3 풀스크린 관련 (0) | 2020.09.21 |