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.
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.
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…
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:
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: