ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Locating and styling a specific word in a textfield.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Craig Kristensen
Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 1:29:13 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,


Return to posts index

Andrei Popa
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 2:35:09 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.


Return to posts index

Craig Kristensen
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 2:44:56 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


Return to posts index


Andrei Popa
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 2:51:34 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.


Return to posts index

Craig Kristensen
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 2:53:55 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!


Return to posts index

Andrei Popa
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 3:04:25 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.


Return to posts index


Craig Kristensen
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 4:34:55 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


Return to posts index

Craig Kristensen
Re: Locating and styling a specific word in a textfield.
on Jan 18, 2019 at 8:25:08 pm

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


Return to posts index

Tomas Bumbulevičius
Re: Locating and styling a specific word in a textfield.
on Feb 13, 2019 at 3:30:58 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.


Return to posts index


Tomas Bumbulevičius
Re: Locating and styling a specific word in a textfield.
on Feb 13, 2019 at 3:32:02 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.


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]