The new text style expressions added in After Effects v17 are powerful, but they can be confusing to learn at first.
This is partly because they break one of the unwritten "rules" of expressions, which is that expressions can only set the value for the property they're on.
For example, an expression on a position property can only set the value for position.
They break this rule due to the fact an expression on a text layer's sourceText property can set either the text value or the text style.
sourceText property object
tstext.sourceText: {// The text content of the stringvalue: string,// The style properties and methodsstyle: object,}
You can set the style of a text layer by returning a style object from an expression on the sourceText property.
sourceText
ts// The text contenttext.sourceText.value; // or text.sourceText// The style objecttext.sourceText.style;
As well as setting a layers style with expressions, you can use text style expressions to retrieve the style properties of a text layer (such as it's font size or leading values).
sourceText
tstext.sourceText.style.fontSize;
The style object
These style objects of sourceText properties contain:
- Methods for setting style attributes
- Properties containing the values of the style attributes
For example, the property fillColor and its matching method setFillColor(), or fontSize and setFontSize().
the style object
jstext.sourceText.style: {fillColor,setFillColor(),fontSize,setFontSize(),// etc}
For a full list of all the properties and methods available on the style object, see the text style page of our expression documentation.
You can use the methods to modify the style's attributes, and the properties to retrieve the style's attributes.
For example, to set a dynamic font size you would use the setFontSize() method:
sourceText
jstext.sourceText.style.setFontSize(36);
Chaining methods
Each style method, e.g. setFont(), returns it's style object (it's this value). This means you can chain the methods to set multiple styles.
sourceText
js// Chaining the methodsotherStyle.setFontSize(36).setLeading(36 * 1.3).setTracking(0);
Setting the text content
Since you set the style by returning a style object on a sourceText property, that means you can't also return a value for the sourceText.value (the text content).
sourceText
jsconst otherStyle = otherLayer.text.sourceText.style;const textContent = thisLayer.name;textContent + "this won't be used";otherStyle; // Overrides the previous line
Learn more about which value is returned from an expression in our article on statement completion values
To get around this, the style object has a setText() method which will set the .value of sourceText.
To set the value of a sourceText property when returning a style object, use the style.setText() method.
sourceText
jsconst otherStyle = otherLayer.text.sourceText.style;const textContent = thisLayer.name;// Return a new style and new text valueotherStyle.setText(textContent);
By default, returning a style object will not override the existing text content, instead it will keep the original value. You only need to use setText() when setting a new value with expressions.
Getting the style at a character index
You can get the style properties at a specific character index with the sourceText method getStyleAt(characterIndex).
sourceText
jstext.sourceText.getStyleAt(8).setFontSize(36);
This returns the style object, so you can then chain the other style methods.
There's no setStyleAt() - it's not possible to set different styles for different portions of the text. The returned style object is used for all characters.
By default, a style object returns the style at the first character, so the following two are equal:
sourceText
js// Both return the style of the first charactertext.sourceText.style;text.sourceText.getStyleAt(0);
Note that getStyleAt() is a method of sourceText properties, not style objects.
Finding font names
It can be difficult to know which string to use as the font name in setFont, as it differs from the name in the character panel.
sourceText
jstext.sourceText.style.setFont("RobotoMono-Medium");
To find the correct name of a font you can use the font selector, found in the expression language menu.
This inserts the name of the selected font into your expression.
Creating style objects
You can create an empty style object by using the sourceText method createStyle().
sourceText
jsconst newStyle = text.sourceText.createStyle();newStyle.setFontSize("90");
For any style properties you haven't set, the value from the original style is used. Because of this, sourceText.createStyle() and sourceText.style are functionally equivalent.
Referencing styles from other layers
You can access the style object of other sourceText properties:
sourceText
jsconst otherStyle = thisComp.layer("text layer").text.sourceText.style;otherStyle;
Which contains all the style attributes of the sourceText.
You can also use specific properties from the style object:
sourceText
jsconst { fontSize, fillColor } =thisComp.layer("text layer").text.sourceText.style;`The layer has a size ${fontSize} and color ${fillColor}`;
Examples
Setting the style and text
sourceText
jsconst otherSourceText = thisComp.layer("layer name").text.sourceText;const textValue = `${thisLayer.name} text`;// Takes the style from another layer// and sets the text (sourceText.value)otherSourceText.style.setText(textValue);
Overriding specific style from another layer
sourceText
jsconst { fontSize, leading } =thisComp.layer("layer name").text.sourceText.style;text.sourceText.style.setFontSize(fontSize).setLeading(leading);
Blog