Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Checking text field for line breaks?

  • Checking text field for line breaks?

    Posted by Cameron Clark on August 12, 2020 at 11:50 pm

    Hi there!

    I’m trying to create some auto formatting for a mograph template that’ll be handed off to an editor, and I’m running into a problem I’m not sure how to solve. Hopefully you expression wizards out there (I’m looking at you, Dan ☺ ) can help me out with the syntax. What I’m looking for is a way to check a text field for line breaks.

    Basically, I’ve got a bullet-point list of items that I want to let the editor edit, while the line spacing between items in the list automatically controlled with expressions based on the number of items in the list and the size of the font. This works great when the items in the list are only one line of text, but if any of these lines of text need to be two lines, my auto-formatting gets all screwed up. So, I’d like to write an expression that checks the text field for a line break, and if it finds one, adds padding to the space between that text field and the next one on the list. I know how to calculate and apply the right sized padding, but I just can’t figure out how to check a text field for line breaks. Is that possible? And if so, what’s the syntax for something like that?

    Thanks!

    Tomas Bumbulevičius replied 6 years ago 3 Members · 4 Replies
  • 4 Replies
  • Andrei Popa

    August 13, 2020 at 6:25 am

    Let’s say you assign your text to variable “txt”. This should return the number of line feeds that your text has

    var newLines = txt.split("\r").length-1;

    If you only want to check if there is a line break, and not the number of them, you can use this

    hasNewLines = txt.indexOf("\r") == -1 ? false : true;

    Andrei
    My Envato portfolio.

  • Cameron Clark

    August 13, 2020 at 4:29 pm

    Worked like a charm! Thank you so much!

    So, the `split` method splits the “txt” string into parts where ever there is a `\r`, and feeds the resulting parts into an array called “newLines.” And then you subtract 1 from the `.length` of the array (which is the number of lines), giving you the number of line breaks.

    Just so I make sure I understand what’s going on here (teach a man to fish and all that), do I have that right?

  • Andrei Popa

    August 14, 2020 at 6:59 am

    That is exactly how it works! Congrats!

    Andrei
    My Envato portfolio.

  • Tomas Bumbulevičius

    August 14, 2020 at 1:19 pm

    Just case – if not a casual line break is added – the ‘r’ regex won’t work in those cases. You need to take into account this in case return carriage is used: ‘\x03’

    Find out more:
    Motion Graphics Design & After Effects Tutorials
    On YT
    On VH

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