Rich text in new UI?

Like say I want a word in my sentence to be bold, better yet, be able to switch that one word to be bold at run time, or highlight to a different color to show that this word has been spoken - much like Karaoke. How would I do this in the new UI system? I’ve searched around on this topic, but it doesn’t seem to be officially addressed anywhere.

I hope this helps. This works like your Karaoke example. It will temporarily “bold” and change the color of each word in a string from beginning to end with some delay between each word. It works by using StringBuilder.Insert to insert rich text opening tags before the word and rich text closing tags after the word. After this is done, the UI text is updated by setting it equal to the StringBuilder.ToString(). The tags are then removed using StringBuilder.Remove in preperation for the next word.

I don’t know if this is the official way of doing this sort of thing, but it does seem to work. It wouldn’t be too hard to use StringBuilder.Replace to make it work on specific words etc…

sBuilder.Replace("words", "<b><color=red>words</color></b>");

Unity Rich Text Manual Page

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Text;

public class RTTest : MonoBehaviour
{
    string boldText = "Those are some bold words!";
    private Text textToDisplay;

    void Start()
    {
        textToDisplay = GameObject.Find("RichTextTest").GetComponent<Text>();
        StartCoroutine(BoldWords(boldText));
    }

    IEnumerator BoldWords(string textToBold)
    {
        string rtOpenTag = "<b><color=green>";   //Rich text opening tag
        string rtCloseTag = "</color></b>"; //Rich text closing tag
        int wordStartIndex = 0;       //Start index of a word
        int wordEndIndex = 0;         //End Index of a word
        float delay = 1.0f;
        StringBuilder sBuilder = new StringBuilder(textToBold);

        while (wordEndIndex < textToBold.Length)
        {            
            wordStartIndex = GetNextStartIndex(textToBold, wordEndIndex);    
            wordEndIndex = GetNextEndIndex(textToBold, wordStartIndex);
            sBuilder.Insert(wordStartIndex, rtOpenTag);                
            sBuilder.Insert(wordEndIndex + rtOpenTag.Length, rtCloseTag);
            textToDisplay.text = sBuilder.ToString();                  
            sBuilder.Remove(wordStartIndex, rtOpenTag.Length);            
            sBuilder.Remove(wordEndIndex, rtCloseTag.Length);                             
            yield return new WaitForSeconds(delay);                      
        }
        textToDisplay.text = sBuilder.ToString(); 
    }

    int GetNextStartIndex(string textToBold, int wordEndIndex)
    {
        int nextStartIndex = wordEndIndex;

        if (textToBold[nextStartIndex] == ' ')
        {
            while (textToBold[++nextStartIndex] == ' '){ }
        }
        return nextStartIndex;
    }

    int GetNextEndIndex(string textToBold, int wordStartIndex)
    {
        int nextEndIndex = wordStartIndex;

        while (++nextEndIndex < textToBold.Length && textToBold[nextEndIndex] != ' ') { }
        return nextEndIndex;
    }
}