Skip to main content
Pouradam
Inspiring
November 10, 2013
Answered

change the font size of a sub-string in a TLF Text

  • November 10, 2013
  • 1 reply
  • 6232 views

I want to decrease the font size of all sub-strings located inside Parenthesis in my TLF Text.

As an example, let's say this is the text in my TLF Text (instance name "testTLF")

this is a (test) and I am test(ing) to find (out) how to resize (text).

I can find and replace all sub-strings inside Parenthesis with a Star (*) with this code:

var insideP:RegExp = /\(([^\)]+)\)/;

while (testTLF.text.search(insideP) != -1){

  testTLF.text =  testTLF.text.replace(insideP,"*");

}

Here is the result:

this is a * and I am test* to find * how to resize *.

Now, the question is how can I RESIZE instead of REPLACE?? to get a result like this:

this is a (test) and I am test(ing) to find (out) how to resize (text).

Thank you SO MUCH for your kind help in this case!


This topic has been closed for replies.
Correct answer Pouradam

recheck my message.


This is the final complete code thank you so much KGlad, I put it all here for other users:

KGlad();

function KGlad():void

{

     var myTextFormat:TextFormat = new TextFormat();

     myTextFormat.size = myText.textFlow.computedFormat.fontSize * 0.8;

     myTextFormat.color = 0xff0000;

    

     resizeParantheticalF(myText,myTextFormat);

          function resizeParantheticalF(tf:TLFTextField,tfor:TextFormat):void

          {

                    var n:int = 0;

                    while (n>=0)

                    {

                              n = tf.text.indexOf("(",n);

                              if (n==-1)

                              {

                                        return;

                              }

                              n++;

                              tf.setTextFormat(tfor,n-1,tf.text.indexOf(")",n)+1);

                    }

          }

}

1 reply

kglad
Community Expert
Community Expert
November 10, 2013

:

function formatParantheticalF(tf:TLFTextField,tfor:TextFormat):void{

    var n:int = tf.text.indexOf("(",0);

    while(n>0){

        n = tf.text.indexOf("(",n);

        n++;

        tf.setTextFormat(tfor,n,tf.text.indexOf(")",n));

    }

}

Pouradam
PouradamAuthor
Inspiring
November 10, 2013

Thanks a lot!! Now at least I know it is possible!!! You helped me a lot

Here is my new code with your kind help:

var myTextFormat:TextFormat = new TextFormat();

myTextFormat.size = myText.textFlow.computedFormat.fontSize * 0.8;

formatParantheticalF(myText,myTextFormat);

 

function formatParantheticalF(tf:TLFTextField,tfor:TextFormat):void

{

         var n:int = tf.text.indexOf("(",0);

          while (n>0)

          {

                         n = tf.text.indexOf("(",n);

                         n++;

                         tf.setTextFormat(tfor,n,tf.text.indexOf(")",n));

          }

}

However, there is still some small problems:

1- the Font of "Parenthesis" does not reduce (only the contents)

2- the part of Text before first " ( ", get resized as well!

this is an Example (parts that resize are shown in bold) :

this is a (test) and I am test(ing) to find (out) how to resize (text).

The question remains whether it is possible we setTextFormat to a specific "RegExp" or not?!

something like this:

var myTextFormat:TextFormat = new TextFormat  ;

var insideP:RegExp = /\(([^\)]+)\)/;

myTextFormat.size = myText.textFlow.computedFormat.fontSize * 0.8;

while (myText.text.search(insideP) != -1)

{

    myText.text = myText.text.setTextFormat(myTextFormat,insideP);

}

(1061: Call to a possibly undefined method setTextFormat through a reference with static type String.)

kglad
Community Expert
Community Expert
November 10, 2013

regex is slow so if i can figure out any other way to use the flash string/array methods/properties, i avoid regex.

so i can't answer that part of your question.

this corrects the code i gave before:

function resizeParantheticalF(tf:TLFTextField,tfor:TextFormat):void{

    var n:int = tf.text.indexOf("(",0);

    while(n>0){

        n = tf.text.indexOf("(",n);

        if(n==-1){

            return;

        }

        n++;

        tf.setTextFormat(tfor,n-1,tf.text.indexOf(")",n)+1);

    }

}