Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Change color of specific text in TextArea component

I have a textArea component that the user will be typing information in.

At some point, say when I click a button, I want to change the color of a specific word in the textArea.

I want to do this without having to convert it to HTML text and using the html tags to do it.

Can anyone help me on this?

Thanks!

M=
0
Mikehd
Asked:
Mikehd
  • 8
  • 5
  • 3
  • +2
1 Solution
 
SavongCommented:
Hmmm, haven't tried the code, but something like this might work:
testString = "found";
indexNumber = myTextField.text.indexOf(testString);
myTextField.setTextFormat (indexNumber, indexNumber+testString.length, myTextFormat)

That code assumes there's already a myTextFormat object set up, it also would only find the first instance of a word, and as I said I haven't tested it yet, but the principle seems sound.  Hopefully it gets you headed the right direction.
0
 
MikehdAuthor Commented:
unfortunatly that does not work.

I had tried this previously using:

_root.the_fmt = new TextFormat();
_root.the_fmt.color=0xFF0000;

writeTextArea.text.setTextFormat(0, 5,_root.the_fmt);


just to test it out I used 0 start ond 5 end positions.


M=
0
 
SavongCommented:
Hmmm, so no response at all?  Does the text format work correctly when applied to the entire text field?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
MikehdAuthor Commented:
It doesn't do the whole text field either. this will do the whole text field:

_root.writeTextArea.setStyle("color", "0xFF0000");

I think I may have to just use an "input text" field rather than the component.
0
 
MikehdAuthor Commented:
..except than it won't scroll if the user types beyond visible area.

ARG!
0
 
SavongCommented:
Hmmm, sorry I've not dug any deeper into that component.  If I come up with anything I'll post, otherwise . . . anyone else?
0
 
nishasrilankanCommented:
0
 
MikehdAuthor Commented:
Thanks nishasrilankan ,

But the whole point of this is that I am trying to avoid having to use HTML formating ect...

M=
0
 
BuffonCommented:
I tried this:
var t:TextFormat = new TextFormat();
t.bold = true;
t.color = 0xff0000;

txt.setTextFormat(1, 4, t);

where txt is dynamic text field, and it worked perfectly.
0
 
nishasrilankanCommented:
Can I know why you dont use HTML formatting...(anyways...to use CSS you dont need to use HTML)...

I think With CSS and a serach and replace function you can achieve this...
0
 
MikehdAuthor Commented:
Buffon,

Yes that works perfectly for a dynamic text field. I am trying to do this on a TextArea component. Where that doesn't work.

0
 
MikehdAuthor Commented:
nishasrilankan,

I am trying to find a way to do this without HTML formating, because that is what I am using now. And the first part works fine. I am matching what the user typed against some other text, and changing the color of the words that don't match. That works.

I was hoping to find a solution that didn't involve adding tags to the text.

But it seems like I will have to make do with what I have, which is OK... but not great.

It would be great if there was something like the setTextFormat() that worked on TextArea components.

M=
0
 
nishasrilankanCommented:
this.createTextField("my_txt", this.getNextHighestDepth(), 0, 0, 100, 22);
my_txt.autoSize = true;
my_txt.text = "Lorem ipsum dolor sit ametdfgdg dfgd dfgdfg...";

var my_fmt:TextFormat = new TextFormat();
my_fmt.bold = true;
my_fmt.font = "Arial";
my_fmt.size = 12;
my_fmt.color = 0xFF0000;

my_txt.setTextFormat(5,15,my_fmt);
0
 
nishasrilankanCommented:
Oh...sorry dude..that isnt work with TextArea...coz' Textareas setTextFormat only have one argument for textFormat

here is a perfect solution for TextField...

TextField.prototype.setStringFormat = function(fObject, string, offset, limit) {
      if (fObject.constructor != new TextFormat().constructor) return undefined;
      if (string == null) this.setTextFormat(fObject);
      var st, count, len = string.length;
      while ((st=this.text.indexOf(string, offset))>-1) {
            offset = st+1;
            count++;
            this.setTextFormat(st, st+len, fObject);
            if (limit>0 && count>=limit) break;
      }
};

_root.createTextField("Txt", 1, 0, 0, 300, 200);
txt1.wordWrap = true;
txt1.text = "Only the word bold in this sentence will be bolded. Only the word bold in this sentence will be bolded. Only the word bold in this sentence will be bolded. Only the word bold in this sentence will be bolded."
tf = new TextFormat();
tf.bold = true;
txt1.setStringFormat(tf, " bold ");
0
 
MikehdAuthor Commented:
nishasrilankan,

I need this to work with TextArea, not TextField...
0
 
negatyveCommented:
_root.the_fmt = new TextFormat();
_root.the_fmt.color=0xFF0000;
writeTextArea.label.setTextFormat(0, 5,_root.the_fmt);

?
0
 
MikehdAuthor Commented:
negatyve,

That did it!!! So it needed "label". Was that in the help and/or documentation because I sure couldn't find it.

I had to do that portion of my project with a workaround so I hope I can find the time to go back and add this. I think I will have to....

Anyway,

THANKS!!!!


M=
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now