How do i change a color of an individual alphabet of a UI.Text
Hello devs Good Evening/Morning. I want to change a color of an individual alphabet of UI Text from scrip using tags of rich color, following is the snippet code I'm trying to get it done with void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Player") { // Destroy(col.gameObject); char cubealphabet=gameObject.GetComponent<AlphabetAssigner>().alphabet.text[0]; Debug.Log(cubealphabet); string currentword = GameObject.Find("GameController").GetComponent<SpawningPlatforms>().currentword; //Debug.Log("current word from destroyer"+GameObject.Find("GameController").GetComponent<SpawningPlatforms>().currentword); if (GameObject.Find("GameController").GetComponent<SpawningPlatforms>().wordbank.checkalphabet(currentword, cubealphabet) == true) { Debug.Log("accurate alphabet"); GameObject.Find("GameController").GetComponent<SpawningPlatforms>().highlightalphabet(cubealphabet); } else { Debug.Log("Incorrect alphabet"); } Destroy(gameObject); } }
the variable cubealphabet just takes a single letter from the gameobject it is on,and it is sent to highlightalphabet function to change its color, following is the snippet code of highlighalphabet function public void highlightalphabet(char colalpha) {
int alphabetindex = wordfield.text.IndexOf(colalpha);
Debug.Log("Alphabet index is " + alphabetindex);
if (alphabetindex == -1)
return;
char coloredalpha=wordfield.text[alphabetindex];
//wordfield.text = "";
for (int i = 0; i <= wordfield.text.Length - 1; i++)
{
//wordfield.text +=currentword[i];
if(i==alphabetindex)
wordfield.text+= "<color=#FF0000>" + coloredalpha + "</color>";
}
}
the problem here is , I can't seem to access an individual character in text with it's respective index like this wordfield.text[i]=""+coloredalpha+""; the error i face here is "can not implicitly convert type string to char" pardon if the question isn't elaborated enough, but the bottom line is, I can't apply richtext properties to an individual alphabets of UI.text.
Answer by Ahndrakhul · Jun 30, 2016 at 08:40 PM
You can use StringBuilder for this sort of thing. You would need to add using System.Text; to your script and then do something like this:
public void highlightalphabet(char colalpha)
{
StringBuilder strBuilder = new StringBuilder(wordfield.text);
strBuilder.Replace(colalpha.ToString(), "<color=#FF0000>" + colalpha + "</color>");
wordfield.text = strBuilder.ToString();
}
This is a really simple example that changes the color of all characters in a string that match the "colalpha" char. It gets more difficult if you want to do things like remove the color change, or change the color of previously altered text, but it's not too hard.
Answer by Bitpocketer · Jul 01, 2016 at 07:07 PM
@Ahndrakhul thank you very much, it did a great job but the problem with it is, it replaces all the occurrences of specified character, my scenario is, let there be a word "Moon", alphabets are floating and user hits 'o' both of those o's are highlighted where as i want just one highlighted o for a single hit, another 'o' has to be highlighted when user hits the o for second time.
I've just realized how bad my original answer is. It will totally fail in the case of your moon example if you hit the m or the n first and then hit the o. It will try to change the color of the o's in the richtext tags. Do you want the o's to be highlighted from left to right, or do you want it to be random?
Try this. It's not great, but it should work with your current setup. It will fail if you try to highlight the "less than" character.
public void highlightalphabet(char colalpha)
{
string word = wordfield.text;
for (int i = 0; i < word.Length; i++)
{
if (word[i] == '<')
{
while (i + 1 < word.Length && word[++i] != '>') { }
}
if (word[i] == colalpha)
{
if (i >= word.Length - 2 || word.Substring(i + 1, 2) != "</")
{
word = word.Remove(i, 1);
word = word.Insert(i, "<color=#FF0000>" + colalpha + "</color>");
break;
}
}
}
wordfield.text = word;
}
Thank you very much, Perfect, It worked exactly the way i wanted it to work, How do i figure out when entire word has been highlighted?