Activity › Forums › Adobe After Effects Expressions › specifying the number of characters in a text layer
-
specifying the number of characters in a text layer
Posted by Billy Schinski on November 9, 2010 at 9:48 pmI’m making a background bar for subtitles. Is there an expression that will allow me to adjust the scale of the bar based on the number of characters contained in my subtitle text layer?
Thank you
David Cabestany replied 12 years, 2 months ago 3 Members · 8 Replies -
8 Replies
-
Dan Ebberts
November 9, 2010 at 10:33 pmIt’s going to look something like this:
max = 10; // characters at 100%
n = thisComp.layer(“Text Layer”).text.sourceText.length;
[(n/max)*value[0],value[1]]The first line defines how many characters correspond to 100% horizontal scale.
Dan
-
David Cabestany
June 13, 2014 at 7:11 pmIs there a way to keep the value of max dependent on the number of characters automatically?
I tweaked the expression to
n = thisComp.layer(index+1).text.sourceText.length;
max =n; // characters at 100%
[(n/max)*value[0],value[1]]Basically I assigned max the same length of my character string instead of an arbitrary value like 10 or 15, but then n and max cancel each other and I always get 100% for scale.
Another problem I’ve found using this approach is that the background layer, in this case a shape layer has a defined scale value when it’s at 100% (whatever I drew originally), so even when I use a set number like 10, it’s only relative to what the original 100% scale is, not to the character string length.
Any help out there?
-
Dan Ebberts
June 13, 2014 at 7:31 pmThe idea for that expression was that you would set up your text layer with a typical number of characters (say 10), scale the background layer to fit, then apply the expression to the background layer. The expression should adjust the width of the background to account for more or less than 10 characters. It’s approximate at best, but the alternatives are ugly unless you’re willing to run a script whenever you change the text.
Dan
-
David Cabestany
June 13, 2014 at 7:42 pmThanks Dan,
I think using a script would be more of a hassle than what the problem actually is.
I’m sorry to be insistent, but I don’t see the point of the expression as it is right now, even when applied, the values are editable by just dragging them and do not seem to be driven by the expression. Most likely I haven’t understood what it does, but nothing seems to be affected.
Thanks.
-
Dan Ebberts
June 13, 2014 at 7:58 pmHmmm. I guess I don’t understand what you’re trying to do. I just tried it, exactly as I posted it above, and when the text string has 10 characters, the expression does nothing to the background layer’s x scale value. When the text has 11 characters, the x scale gets scaled up to 110% of whatever it was before.
Dan
-
David Cabestany
June 13, 2014 at 10:36 pmHere’s the main problem I’m running into:
My longest word is Philippines, which is 11 characters, so I should be drawing a box that covers all that and set the max amount to 11. That works perfect, but the scale is 100%, nothing has been modified.
Then when I change the word to Japan, for example, the scale doesn’t cover the whole word (please see attached), in the same fashion, if I type anything longer it doesn’t work either.
Perhaps is because the expression can’t account for the different space that each letter occupies?
I ended up using a completely different solution, provided by Kevin Camp, which actually works better because it takes care of whatever difference each letter may have in shape and thickness, so the bounding box is always precise regardless of the length of the word, but if you don’t mind I’d like to understand why this is not working as it’s supposed to (most likely because I’m doing it wrong).
Thanks again!
max = 11; // characters at 100%
n = thisComp.layer("txt").text.sourceText.length;
[(n/max)*value[0],value[1]] -
Dan Ebberts
June 13, 2014 at 10:56 pm>Perhaps is because the expression can’t account for the different space that each letter occupies?
Exactly. It doesn’t work well if character sizes vary greatly. “PHILIPPINES” isn’t a good template because of all the I’s. If you want to set it up for worst case, use something like “https://WWWWWWWWWWW” or “MMMMMMMMMMM”. I would probably use “XXXXXXXXXXX”.
If you always use the same font, another approach that can be very accurate is to build a table of character widths.
Then there’s always the sampleImage() hack, which is usually accurate, but slow.
Dan
-
David Cabestany
June 16, 2014 at 3:59 pmThat’s a great suggestion, I’ll keep it mind for future occasions. Thanks for your help and patience.
For anyone that might be experiencing the same issue, take a look at this thread instead, Kevin Camp’s method worked perfectly.
https://forums.creativecow.net/thread/227/27238#27247
Reply to this Discussion! Login or Sign Up

