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

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

Participant ,
Oct 09, 2020 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

4.4K

Translate

Translate

Report

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 2 Correct answers

Valorous Hero , Oct 09, 2020 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 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)

 

 

Votes

Translate

Translate
Valorous Hero , Oct 13, 2020 Oct 13, 2020

WOOTS!!! This seems to work.

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

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

Votes

Translate

Translate
Enthusiast ,
Oct 09, 2020 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?

Votes

Translate

Translate

Report

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 09, 2020 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.

Votes

Translate

Translate

Report

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
Enthusiast ,
Oct 09, 2020 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);

 

 

Votes

Translate

Translate

Report

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 09, 2020 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 😞

Votes

Translate

Translate

Report

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
Enthusiast ,
Oct 09, 2020 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?

Votes

Translate

Translate

Report

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
Explorer ,
Apr 16, 2023 Apr 16, 2023

Copy link to clipboard

Copied

LATEST

I'm no expert but I think you must apply multiple properties by continuing the expression as one single line. You can format the line (using whitespace) to help read it better. The following line tells the current text layer to mimic the text and properties of another "BasicText" layer:

 

text.sourceText.style.setText(thisComp.layer("BasicText").text.sourceText)
  .setAutoLeading(false)
  .setFont(thisComp.layer("BasicText").text.sourceText.style.font)
  .setFontSize(thisComp.layer("BasicText").text.sourceText.style.fontSize)
  .setLeading(thisComp.layer("BasicText").text.sourceText.style.leading)
  .setFillColor(thisComp.layer("BasicText").text.sourceText.style.fillColor)
  .setStrokeColor(thisComp.layer("BasicText").text.sourceText.style.strokeColor)
  .setStrokeWidth(thisComp.layer("BasicText").text.sourceText.style.strokeWidth);

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 09, 2020 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.

Votes

Translate

Translate

Report

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
Valorous Hero ,
Oct 09, 2020 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)

 

 

Motion Graphics Brand Guidelines & Motion Graphics Responsive Design Toolkits

Votes

Translate

Translate

Report

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 11, 2020 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!

Votes

Translate

Translate

Report

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
Valorous Hero ,
Oct 12, 2020 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.

Motion Graphics Brand Guidelines & Motion Graphics Responsive Design Toolkits

Votes

Translate

Translate

Report

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
Valorous Hero ,
Oct 13, 2020 Oct 13, 2020

Copy link to clipboard

Copied

WOOTS!!! This seems to work.

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

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

Motion Graphics Brand Guidelines & Motion Graphics Responsive Design Toolkits

Votes

Translate

Translate

Report

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