Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro TextMesh Pro - change bold/underline at runtime

Discussion in 'UGUI & TextMesh Pro' started by rames44, Feb 3, 2018.

  1. rames44

    rames44

    Joined:
    Mar 24, 2017
    Posts:
    6
    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.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Add the following script to a TMP text object.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using TMPro;
    6. public class ExampleScript : MonoBehaviour
    7. {
    8.     // Reference to the TMP text component.
    9.     // The TMP_Text class is the base class for both TMP text components.
    10.     // These are <TextMeshPro> and <TextMeshProUGUI>
    11.     public TMP_Text TextComponent;
    12.     private void Awake()
    13.     {
    14.         // Assign the underline style to the text component.
    15.         TextComponent.fontStyle = FontStyles.Underline;
    16.     }
    17. }
    18.  
    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.
     
    rbitard, AquaAF, Aced_Gamedev and 5 others like this.
  3. rames44

    rames44

    Joined:
    Mar 24, 2017
    Posts:
    6
    Brilliant - thank you.

    Be kind of nice if the scripting section of http://digitalnativestudios.com/textmeshpro/docs/ 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...
     
  4. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    If you need to set multiple flags on the font style at the same time, use code like the following

    Code (csharp):
    1.  
    2. TMP.fontStyle = FontStyles.Underline | FontStyles.Bold | FontStyles.SmallCaps;
    3.  
     
  5. kreso

    kreso

    Joined:
    Sep 7, 2013
    Posts:
    144
    To test if it's bold:
    Code (CSharp):
    1.  
    2. bool isBold = (tmPro.fontStyle & FontStyles.Bold) != 0;
    3.  
    To remove ONLY bold:
    Code (CSharp):
    1.  
    2. tmPro.fontStyle ^= FontStyles.Bold;
    3.  
     
    Last edited: Jul 23, 2019
    ayadkk1988, rbitard, glumleaf and 7 others like this.
  6. EskAere

    EskAere

    Joined:
    Jan 11, 2017
    Posts:
    7
    Hello !

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

    Thanks :)
     
  7. Planks4ever

    Planks4ever

    Joined:
    Feb 2, 2018
    Posts:
    1
    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!
     
  8. mu_ki

    mu_ki

    Joined:
    Feb 20, 2018
    Posts:
    1
    Code (CSharp):
    1. tmPro.fontStyle ^= FontStyles.Bold;
    works perfectly in my project.
     
  9. sdalex

    sdalex

    Joined:
    Dec 17, 2014
    Posts:
    21
    So to unset a FontStyle properly:
    Code (CSharp):
    1. bool isSet = (tmPro.fontStyle & FontStyles.Bold) != 0;
    2. 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:
    Code (CSharp):
    1. tmPro.fontStyle ^= FontStyles.Bold;
    Since the property "fontStyle" can have multiple styles, the following just remove all styles except of the "FontStyles.Bold":
    Code (CSharp):
    1. tmPro.fontStyle &= FontStyles.Bold;
    The following is adding a style in case it was not set (and doesn't remove the other styles):
    Code (CSharp):
    1. tmPro.fontStyle |= FontStyles.Bold;
    The following remove all styles and set the "FontStyles.Bold" only:
    Code (CSharp):
    1. tmPro.fontStyle = FontStyles.Bold;
     
    Last edited: Jun 1, 2022
  10. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    325
    Since ^ is a toggle operator, you can do the same operation without the check

    You can remove a flag with the not operator
    Code (CSharp):
    1. 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/