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:
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";
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?
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.
Which property of the text layer is the expression set on? If it's the "Source Text" property, I think you can just do:
Yeah, it works with just "style.setFontSize(25);" as well...but not in combination wit a expression set text 😞
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?
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.
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.
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
// get style of source or originating text layer
// declare style of source or originating text layer, get it's text string, and conform text string to a 25 fontSize
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!
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.
WOOTS!!! This seems to work.
text.sourceText.style.setFontSize(25).setText("This should be way smaller")