PDA

View Full Version : Simple htmltext control


john100
02-20-2006, 05:50 AM
Hi again,

I am trying to create html text into a control.
I cannot use this line below, it give error=> ecounter "<b>" expecting "/Script"
txt.htmlText = "<b>fassdfsdfdsf</b>";

however, if I use this line below(without html tag), it is fine
txt.htmlText = "fassdfsdfdsf";

any one know how to embedded html to the text/control or anything that able to produce html.

There is a tag call <mx:htmlText> => but I cannot find it in actionscript

public function initApp():void
{
var n:VBox = new VBox();
var txt:Text = new Text();
txt.html = true;
txt.htmlText = "<b>fassdfsdfdsf</b>";
n.addChild(txt);
this.addChild(n);
}

Flash Gordon
02-20-2006, 06:05 AM
_root.createTextField("_txt", 0, 0, 0, 0, 0);
_txt.autoSize = true;
_txt.border = true;
_txt.html = true;
_txt.htmlText = "<b>Hello World</b>"+" Hello World";


Works fine.

hangalot
02-20-2006, 04:41 PM
ignore what flash gorden said, he is living the old style there ;)


var n:TextArea = new TextArea();
n.htmlText = "<p> kjasj</p><br/><p>more random text</p>";
this.addChild(n);


i think most of the text components have a htmlText property and using that makes it a html container by default. styling these were an issue in flex 1.5 (not as straight foreward as other components) but i have not checked this with flex 2 yet

hangalot
02-20-2006, 04:43 PM
notice what i did here, i used textarea, label will defnitly work as well. i would not use text on its own (you have the flex fraework already so use it!)

Tink
02-21-2006, 02:09 AM
or in AS :)

import flash.display.TextField;
import flash.display.TextFieldAutoSize;

private function createTextField():void
{
var myText = new TextField();
myText.autosize = TextFieldAutoSize.LEFT;
myText.html = true;
myText.htmlText = "<p> kjasj</p><br/><p>more random text</p>";
addChild( myText );
}

also Text and TextArea are all part of the flex framework. in fact Text extends Label. So Label would be the lowest level to go to create a simple field.

Flash Gordon
02-21-2006, 02:24 AM
ignore what flash gorden said, he is living the old style there ;)

Oops.....Sorry I didn't see it was AS 3.0.

john100
02-21-2006, 05:37 AM
I found out the reason why most of the time, it prompt me "/Script" error.

When I didnot wrap the <mx:script> with "<![CDATA[". It give an error. But if I wrapped. It is fine.

However, I am still not able to display the text with this below code.
(You can try to copy paste and run it. It showing up nothing.)


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" layout="absolute" creationComplete="createTextField()">
<mx:Script>
<![CDATA[
import flash.display.TextField;
import flash.display.TextFieldAutoSize;
private function createTextField():void
{
var myText:TextField = new TextField();
myText.autoSize = TextFieldAutoSize.LEFT;
myText.html = true;
myText.htmlText = "<p> kjasj</p><br/><p>more random text</p>";
this.addChild( myText );
}
]]>
</mx:Script>
</mx:Application>


However, if I run this code below by commenting out myText, it show the box, which means, I code it correctly, except for myText area.
You can copy paste the code and try it.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" layout="absolute" creationComplete="createTextField()">
<mx:Script>
<![CDATA[
import flash.display.TextField;
import flash.display.TextFieldAutoSize;
import mx.containers.VBox;
private function createTextField():void
{
var n:VBox = new VBox();
var myText:TextField = new TextField();

n.setStyle("borderThickness", "5");
n.setStyle("borderStyle", "solid");
n.setStyle("backgroundColor", "0x5a6970");
n.x = 200;
n.y = 200;
n.width = 200;
n.height = 200;

//myText.autoSize = TextFieldAutoSize.LEFT;
//myText.html = true;
//myText.htmlText = "<p> kjasj</p><br/><p>more random text</p>";
//n.addChild(myText);
this.addChild( n );

}
]]>
</mx:Script>
</mx:Application>


And thanks again for the help. I starting be able to progress. I wish Macromedia provides more simple example which able to be run. That's will be helpful. Sometimes, I didnot know, is it because of beta it is cannot be run or is it I am not coding it correctly.

john100
02-21-2006, 05:05 PM
In case any body needed. Need to use TextArea as suggested by hangalot.
Using text field will not working, but who knows it work in other area.

Here is the working code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" layout="absolute" creationComplete="createTextField()">
<mx:Script>
<![CDATA[
import mx.controls.TextArea;
private function createTextField():void
{
var myText:TextArea = new TextArea();
myText.html = true;
myText.htmlText = "<p> kjasj</p><br/><p>more random text</p>";
this.addChild( myText );
}
]]>
</mx:Script>
</mx:Application>

hangalot
02-21-2006, 10:13 PM
its because you are appending it to a mx:Application. as tink pointed out to me the other day sprites and the like attach fine to the display tree in a canvas while when trying to attach them to a application object doesn't work. it wants something that extends the flex framework

here is a workaround i use for that though

var p:AllTestGui = new AllTestGui();
this.allChildrenList.addChild(p);

Tink
02-22-2006, 12:42 PM
i wouldn't really use TextField with the rest of the Flex framework useless i was developing my own component.

so as hangalot mentioned presviously you'd be best using a label here.<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns="*" layout="absolute" creationComplete="onCreationComplete()">

<mx:Script>
<![CDATA[
private function onCreationComplete():void
{
label.text = "<p> kjasj</p><br/><p>more random text</p>";
}
]]>
</mx:Script>


</mx:Label id="label"/>
</mx:Application>

Tink
02-22-2006, 12:46 PM
When I didnot wrap the <mx:script> with "<![CDATA[". It give an error. But if I wrapped. It is fine.Flex builder should create this stuff for you.

type '<mx:Sc' and it should show the script tag. Hit enter to add it to your code, and you will get.

'<mx:Script'

close the opening tag with '>' and it should add the CDATA and Script closing tag for you.

john100
02-22-2006, 08:23 PM
Good stuff. Thanks again for all the help