(Expressions) Setting Font Size and Content of a text layer

Participant ,
Oct 09, 2020

Copy link to clipboard

Copied

I'm trying to set my font size based on the content of my text layer . It should interweave with sourceRectAtTime().width in the end to scale (but using the font size and not scale) it the text goes out of bounds. But my problem seems to be a little bit more basic than that dependence:

 

I dont have a problem setting the font size with an expression if I use the source text written by hand:

text.sourceText.style.setFontSize(25);

But when I try to set my text via expression, the FontSize command won't work anymore:

text.sourceText.style.setFontSize(25);
text.sourceText.value = "This should be way smaller";

 

Corvin0_0-1602247725086.png

 

TOPICS
Error or problem, Expressions, How to, Scripting

Views

229

Likes

Translate

Translate

Report

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

2 Correct Answers

Adobe Community Professional , Oct 13, 2020
Roland Kahlenberg Adobe Community Professional , Oct 13, 2020
WOOTS!!! This seems to work. ----------------------------- text.sourceText.style.setFontSize(25).setText("This should be way smaller")

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional , Oct 09, 2020
Roland Kahlenberg Adobe Community Professional , Oct 09, 2020
The following should work – ensure you apply this Expression to the 'child/copy' of the original/source/sauce Text Layer. // get source or originating text layer's text stringsauceText=thisComp.layer("sauce").text.sourceText;   // get style of source or originating text layersauceTextStyle=sauceText.style;   // declare style of source or originating text layer, get it's text string, and conform text string to a 25 fontSizesauceTextStyle.setText(sauceText).setFontSize(25)    

Likes

Translate

Translate
Jump to answer Jump to answer
Enthusiast ,
Oct 09, 2020

Copy link to clipboard

Copied

You may need to set the value sourceText before you set the font size.  What happens when you use .setFontSize(25) after you set the sourceText.value?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Participant ,
Oct 09, 2020

Copy link to clipboard

Copied

Then it wont recognise the new text at all and falls back to the original source text. Tried it with "text.sourceText.text", "text.sourceText.value", "value" and simply the text under quotes.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Oct 09, 2020

Copy link to clipboard

Copied

Which property of the text layer is the expression set on?  If it's the "Source Text" property, I think you can just do:

 

style.setFontSize(25);

 

instead of:

 

text.sourceText.style.setFontSize(25);

 

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Participant ,
Oct 09, 2020

Copy link to clipboard

Copied

Yeah, it works with just "style.setFontSize(25);" as well...but not in combination wit a expression set text 😞

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Oct 09, 2020

Copy link to clipboard

Copied

I think you may need to have a null layer, and apply the expression to that, but target the text layer in that expression.  Which property is your current expression applied to again?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Oct 09, 2020

Copy link to clipboard

Copied

Cropped screenshots that don't show everything you've done to the layer giving you problems are not at all helpful in figuring out the problems. Partial expressions don't work either. You talk about using sourceRectAtTime() but don't show how. 

 

I'm not clear on your workflow or which property is being driven by sourceRectAtTime() so I can't be of much help. If you are trying to fit the text to a specific sized box the simplest approach would be to compare the width of the text to the width of the box and adjust the scale. The math to convert point size to pixels is not accurate because 25 pt type for the word TEXT using Open Sans is going to be a completely different size than the word TEXT using Onyx. The point size does not take into account any tracking or kerning values or font scale. Just look at the size of the sample text in the Text options. None of the sample text is the same width.

Screenshot_2020-10-09 09.30.51_QwTm5j.png

I have no idea how you can accurately convert point size into text area width and height. A string of 1111 is going to be a lot narrower than a string of 0000 unless you are using a monospaced font so even the characters in the line of text will change the width.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Oct 09, 2020

Copy link to clipboard

Copied

The following should work – ensure you apply this Expression to the 'child/copy' of the original/source/sauce Text Layer.

// get source or originating text layer's text string
sauceText=thisComp.layer("sauce").text.sourceText;

 

// get style of source or originating text layer
sauceTextStyle=sauceText.style;

 

// declare style of source or originating text layer, get it's text string, and conform text string to a 25 fontSize
sauceTextStyle.setText(sauceText).setFontSize(25)

 

 

<br>Development | Consulting | Training>Motion Graphics Brand Guidelines & Motion Graphics Responsive Design Solutions <br><br>Development | Consulting | Training

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Participant ,
Oct 11, 2020

Copy link to clipboard

Copied

Thanks for the replies guys. Sorry for my late reply, you know...weekends 😉

 

Rick, you are absolutely right - I just like to give some background information what my "reduced" problems were aiming to, so its more understandable why I do some things. But you are right regarding sourceRectAtTime() - I don't show exactly what I'm up to. Thats because the problem occurs even without it. That would be only my step 2.

 

And of yourse you are correct regarding font width/monospace. I just wandet to try to measure and react the widht, maybe not even with a scale but with deletion of characters if they exceed the container eg.g

 

Roland, thanks for the idea with the child. I still hope there is some way to solve this in a single textlayer, but that's a great idea for a workaround!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Oct 12, 2020

Copy link to clipboard

Copied

Thanks for reporting back.

 

I doubt there is a one-layer solution. As it is, you're going through an awkward by requiring the text to be input directly into the sourceText property's Expression and then applying a fontSize to it there. There are more efficient ways to accomplish what you've stated but you've also stated that this is only one part of what you want to accomplish ... so, the solution provided isn't ideal for 'normal' use-cases too. 🙂

For an elegant and straight forward solution, the text can be input into the Essential Graphic's (EGP) sourceText Property and then use either Transform>Scale or enable the Font's Edit Properties to allow for Font Size adjustments in the EGP.

<br>Development | Consulting | Training>Motion Graphics Brand Guidelines & Motion Graphics Responsive Design Solutions <br><br>Development | Consulting | Training

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Oct 13, 2020

Copy link to clipboard

Copied

WOOTS!!! This seems to work.

-----------------------------

text.sourceText.style.setFontSize(25).setText("This should be way smaller")

<br>Development | Consulting | Training>Motion Graphics Brand Guidelines & Motion Graphics Responsive Design Solutions <br><br>Development | Consulting | Training

Likes

Translate

Translate

Report

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