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.
Answer by Ahndrakhul · May 19, 2016 at 07:18 AM
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>");
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;
}
}
Wow, I really messed up reading the docs! I assumed the new UI text wouldn't support rich text because I couldn't find any mention of it.
Thanks for your examples and bringing it to my attention though! :)
$$anonymous$$y problem is that...when I touch on UI Text area a particular word is not selected...in Unity Android...what can I do for this situation...please help me....
Your answer
Follow this Question
Related Questions
How to write actual text into game play 2 Answers
problem with Adjustable Character Spacing script in Unity 5.4 0 Answers
Unity 5 - Does anyone know how to scale the text in the UI? 1 Answer
Resizing font by scale according to screen size, (More complicated than you might think) 1 Answer
Marquee/scrolling text with Unity 4.6 UI 2 Answers