Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

getting text width of specific lines?

New Here ,
Apr 12, 2019 Apr 12, 2019

hello-

first time posting, be gentle. I'm playing around with good old sourceRectAtTime and while I can get the basics for what I need, I'm wondering if it would be possible to find the width of specific lines of text, between carriage returns? in other words, if I have one text layer that is multiple lines (potentially) but I want to have a shape behind each one that actually corresponds to the width of the specific line it is behind, is that possible? my alternative is of course to utilize multiple text layers but I'd rather be able to automate it so a user can input text with carriage returns and it is all straightforward that way.

any help would be great, thanks!

TOPICS
Expressions
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 13, 2019 Apr 13, 2019

Long and short: Nope. sourceRectAtTime() is exactly that - a layers content bounding box, nothing more. there is no way to derive anything more from it because those values you would need simply don't exist in a form that would be accessibel to expressions. As it stands, your only option would be to do the calculations yourself using string operations and adding known widths of letters up.

Mylenium

Translate
LEGEND ,
Apr 13, 2019 Apr 13, 2019

Long and short: Nope. sourceRectAtTime() is exactly that - a layers content bounding box, nothing more. there is no way to derive anything more from it because those values you would need simply don't exist in a form that would be accessibel to expressions. As it stands, your only option would be to do the calculations yourself using string operations and adding known widths of letters up.

Mylenium

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 16, 2019 Apr 16, 2019

yeah, that's what i sorta figured. thanks for your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 26, 2021 Oct 26, 2021

I figured this one out by using the Javascript "split" function to divide up the main SourceText property by carriage returns. You do have to use multiple text layers, but they can work in the background and the user can still just be given a single entry field.

Create as many additional text layers as you think you might have lines in an extreme scenario. Add an expression to the source text of each of those layers that's something like this:

textVal=thisComp.layer("Main User-Controlled Text Layer").text.sourceText;
linesVal=textVal.split("\r");
linesVal[0];

The key here is knowing that "\r" signifies a carriage return. With the text divided in that way, just grab a different piece of the resulting array on each layer. Then, wherever you're needing to identify the individual line widths, just reference those helper layers (i.e. "thisComp.layer("Line Width 1").sourceRectAtTime().width;")

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 28, 2021 Oct 28, 2021

This surely works, as long as you know user used only return carriages.

Make sure to check "shift+return" split case as well, which stands for: \x03

 

Other than that, there is also \n next line indicator, but it might be not used in AE's texts. These are three common next-line indicators from text editors.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 19, 2024 Sep 19, 2024
LATEST

Hello,

A bit late but I found a way to do this by cheating and I want to share it.

You use the split function and change the content of the text layer after the out point.

I explain :

##### code in text source #####

textVal = text.sourceText;

linesVal=textVal.split("\r");

for(i=0 ; i<linesVal.length ; i++){

if (time>=thisLayer.outPoint+i+1){

textVal = linesVal[i] }

}

textVal

##########

By this way I transform my text content each second after the out-point (so we'll never see that text) with the content of one line (the first line at 1 second after out point, the line 2 at 2 second after out point, etc.)

After that you just have to get the sourceRectAtTime width at the time corresponding to the line content :

##### code to get the width #####

thisComp.layer("mytextLayer").sourceRectAtTime(thisComp.layer("mytextLayer").outPoint+1,false).width;

 

Hope that could help somebody

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines