TextMesh Pro - change bold/underline at runtime

I fully understand that TextMesh Pro supports embedding character formatting codes within the text. However, I can’t find any way to alter the bold/underline settings for the entire object at runtime. In other words, how do I do the equivalent of manipulating the “Font Style” buttons at runtime through the TextMeshProUGUI object?

Example: having a script that detects when you’re hovering over a TMP image and setting the text to be underlined.

Add the following script to a TMP text object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ExampleScript : MonoBehaviour
{
    // Reference to the TMP text component. 
    // The TMP_Text class is the base class for both TMP text components.
    // These are <TextMeshPro> and <TextMeshProUGUI>
    public TMP_Text TextComponent;
    private void Awake()
    {
        // Assign the underline style to the text component.
        TextComponent.fontStyle = FontStyles.Underline;
    }
}

There are different ways to alter the visual appearance of the text when interacting with it. See Example 12 and 12a included with TextMesh Pro.

When looking at example 12, hold the right shift key to interact with individual characters.

9 Likes

Brilliant - thank you.

Be kind of nice if the scripting section of TextMesh Pro Documentation were up to date. It does not appear to reflect the current class structure. Mind you, the parts about using it through the Unity editor are great - ditto to the same sections in the bundled user guide. But some proper scripting documentation would be nice…

If you need to set multiple flags on the font style at the same time, use code like the following

TMP.fontStyle = FontStyles.Underline | FontStyles.Bold | FontStyles.SmallCaps;
28 Likes

To test if it’s bold:

bool isBold = (tmPro.fontStyle & FontStyles.Bold) != 0;

To remove ONLY bold:

tmPro.fontStyle ^= FontStyles.Bold;
11 Likes

Hello !

How do you manage to do bitwise operation ?
The FontStyles enum is not marked by the [Flags] attribute.

Thanks :slight_smile:

Sorry for the necromancy, but this is the first result that comes up when I try to search for info regarding this, and wanted to help the next person who searches this.

the method that user kreso mentioned to remove only 1 style doesn’t work. Perhaps it was a typo, but the correct operator to use in this case is:

tmPro.fontStyle &= FontStyles.Bold;

Hope this helps out the next person!

tmPro.fontStyle ^= FontStyles.Bold;

works perfectly in my project.

So to unset a FontStyle properly:

bool isSet = (tmPro.fontStyle & FontStyles.Bold) != 0;
if(isSet) tmPro.fontStyle ^= FontStyles.Bold;

The “^” is the XOR operator. The follow doesn’t remove the FontStyles.Bold style, but invert it instead. So if it was set, it will unset it but if it was not set, it will set it:

tmPro.fontStyle ^= FontStyles.Bold;

Since the property “fontStyle” can have multiple styles, the following just remove all styles except of the “FontStyles.Bold”:

tmPro.fontStyle &= FontStyles.Bold;

The following is adding a style in case it was not set (and doesn’t remove the other styles):

tmPro.fontStyle |= FontStyles.Bold;

The following remove all styles and set the “FontStyles.Bold” only:

tmPro.fontStyle = FontStyles.Bold;
5 Likes

Since ^ is a toggle operator, you can do the same operation without the check

You can remove a flag with the not operator

tmPro.fontStyle &= ~ FontStyles.Bold

Alan Zucconi has a great article about flag arithmetics
https://www.alanzucconi.com/2015/07/26/enum-flags-and-bitwise-operators/