Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic text and setNewTextFormat...

Status
Not open for further replies.

SFiend

Programmer
Jun 4, 2004
39
CZ
Hi,
I have dynamic text a I need to make one line red, next one blue, next one black etc. But when I change color or italic style, Flash change all in the text box. Not only the new text. Where is a problem? Thanks...

function InsertText(NewText, NewColor, Italic) {
var NewFormat:TextFormat = new TextFormat();
NewFormat.italic = Italic;
NewFormat.color = NewColor;

_root.History.setNewTextFormat(NewFormat);
_root.History.text += NewText;
_root.History.scroll = _root.History.maxscroll;
}
 
You cannot apply a TextFormat to an individual line within a TextField, but only to a TextField as a whole. Therefore if you create a new TextFormat object and apply it to a TextField, the entire text in the TextField will be formatted in the new style.

One way to deal with this is to use the htmlText property of the TextField. For example:
[tt]//
function InsertText(NewText:String, NewColor:String, Italic:Boolean) {
_root.History.html = true;
var txt:String = "<font color='"+NewColor+"'>";
if (Italic) {
txt += "<i>";
}
txt += NewText;
if (Italic) {
txt += "</i>";
}
txt += "</font>";
_root.History.htmlText += txt;
}
//[/tt]
If I want to add a new line of text in red and italic, call the function:
[tt]//
InsertText("New line in red and italic", "#ff0000", true)
//[/tt]
The function will create this HTML and added to the TextField "History":
[tt]<font color='#ff0000'><i>New line in red and italic</i></font>[/tt]

Hope this is what you're after!

Kenneth Kawamoto
 
Thank you for you answer, but it still doesn't work :(

I insert text as html, but it still print only black letters and no italic style. I don't know why :(
 
Apply format to selected portion only...

To set formatting on only one part of a textfield, specify the start and end position to which the formatting should be applied. To underline only the words "underlined text" in the following, apply the format only to that section of text:
Code:
this.createTextField("tfNewfield",1,10,10,300,30);
tfNewfield.text = "Here's some underlined text and here's some not";
rf = new TextFormat();
rf.size = 12;
rf.font = "Arial";
tfNewfield.setTextFormat(rf);
uf = new TextFormat();
uf.underline = true;
uf.size = 12;
uf.font = "Arial";
tfNewfield.setTextFormat(12, 27, uf);



Regards. Affiliate Program - Web Hosting - Web Design
After 25,000 posts, banned on FK, but proud not to have bowed down to them!
 
That's true. So, SFiend, you can modify your function like this:
[tt]//
function InsertText(NewText:String, NewColor:Number, Italic:Boolean) {
var NewFormat:TextFormat = new TextFormat();
NewFormat.color = NewColor;
NewFormat.italic = Italic;
var index1:Number = _root.History.text.length;
_root.History.text += NewText;
var index2:Number = _root.History.text.length;
_root.History.setTextFormat(index1, index2, NewFormat);
}
//[/tt]

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top