Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Locating and styling a specific word in a textfield.

  • Locating and styling a specific word in a textfield.

    Posted by Craig Kristensen on January 18, 2019 at 1:29 pm

    Hi everyone.

    I have a super specific problem that I just can’t figure out.

    I have a dynamic texfield (“TextA”)that get populated via a JSON file and I need to find a way to select one of the words and change the colour.
    I have a seperate textfield (“TextB”) that also gets populated via JSON which contains the specific word.

    TextA:
    “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas rutrum arcu maximus hendrerit ullamcorper. Suspendisse lobortis odio nis.”

    TextB:
    “Maecenas”

    Now I need to search TextA for the word “Maecenas” and then select it (probably using an animator) and then give it a different colour.

    Can this even be done?
    Any ideas?

    Cheers,

    Tomas Bumbulevičius replied 7 years, 6 months ago 3 Members · 9 Replies
  • 9 Replies
  • Andrei Popa

    January 18, 2019 at 2:35 pm

    Apply a color animator to your textA with the desired color for the word. Go to “Animator>Advanced” and change “Units” to “Index” and “Based on” to “Characters”

    Then, on range selector add this expressions.

    This to start:
    thisLayer("Text")("Source Text").indexOf(thisComp.layer("textB")("Text")("Source Text"))

    And this to End:

    redWord = thisComp.layer("textB")("Text")("Source Text");
    thisLayer("Text")("Source Text").indexOf(redWord)+redWord.length

    Andrei
    My Envato portfolio.

  • Craig Kristensen

    January 18, 2019 at 2:44 pm

    Wow Andrei ! That works exactly as intended!
    Will I be able to add an if/else statement to this as well?

    If textB is n’t in the textA, then is simply colours the first x amount of characters instead.
    I think an if/else could fix this?

    cheers,
    Craig

  • Andrei Popa

    January 18, 2019 at 2:51 pm

    redWord = thisComp.layer("textB")("Text")("Source Text");
    if ( thisLayer("Text")("Source Text").indexOf(redWord) > -1){
    thisLayer("Text")("Source Text").indexOf(redWord)
    }else{
    0
    }

    and
    redWord = thisComp.layer("textB")("Text")("Source Text");
    if ( thisLayer("Text")("Source Text").indexOf(redWord) > -1){
    thisLayer("Text")("Source Text").indexOf(redWord)+redWord.length
    }else{
    0
    }

    But still does not work well if you have enters in your text. Was trying for a solution because split(“\n”) did not seem to work. Will post here if i manage to find one.

    Andrei
    My Envato portfolio.

  • Craig Kristensen

    January 18, 2019 at 2:53 pm

    It seems to work perfectly… I’ll put it through some testing and see how it handles it.
    Thank you so much Andrei!
    This is SUCH a big help!

  • Andrei Popa

    January 18, 2019 at 3:04 pm

    Update. On my system, this did not work if there were “enter” characters before the highlited word. Every “enter” would offset the highlight by 1. Seems like after effects uses “\r” character for new lines. So this is the final form of the expressions.
    For start:
    redWord = thisComp.layer("textB")("Text")("Source Text");
    newLines = thisLayer("Text")("Source Text").split("\r").length -1;
    if ( thisLayer("Text")("Source Text").indexOf(redWord) > -1){
    thisLayer("Text")("Source Text").indexOf(redWord)-newLines;
    }else{
    0
    }

    And for end:
    redWord = thisComp.layer("textB")("Text")("Source Text");
    newLines = thisLayer("Text")("Source Text").split("\r").length -1;
    if ( thisLayer("Text")("Source Text").indexOf(redWord) > -1){
    thisLayer("Text")("Source Text").indexOf(redWord)+redWord.length-newLines
    }else{
    0
    }

    Andrei
    My Envato portfolio.

  • Craig Kristensen

    January 18, 2019 at 4:34 pm

    Ah, I see what you mean.
    THe latest version works perfectly and fixes the “enter” issue.

    Thank you again Andrei! You have been super helpful!
    Cheers,
    Craig

  • Craig Kristensen

    January 18, 2019 at 8:25 pm

    This is a long shot. But can you change the style (regular, bold, italic) also, using this method?

  • Tomas Bumbulevičius

    February 13, 2019 at 3:30 pm

    In case anyone face situation where ‘Shift+Enter’ break causes problems – add into equation ‘\x03’ for start and end newLines calculation.

    Craig – its not possible to adjust ‘font’ directly. But to this Text Animator, you could add stroke width (as long as stroke is applied to text), skew, etc – and that would allow you to make visual differences for selected range of text.

    newLines = curText.split("\r").length -1 + curText.split("\x03").length -1;

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.

  • Tomas Bumbulevičius

    February 13, 2019 at 3:32 pm

    Sorry, assuming you define ‘curText’ as below!

    curText = thisLayer("Text")("Source Text");
    newLines = curText.split("\\r").length-1 + curText.split("\x03").length -1;

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy