- Home /
EditorWindow : Change the color of words in a Label String
Hello everyone :)
Here is what I developped to find words in Texts or TextsMeshPro from the scene. But I would like to change the color of words found, like in the second image.
What I'd like to have :
How could I change the color of words in a string ? What I have right now is a label like that :
var areaStyle = new GUIStyle(GUI.skin.label);
areaStyle.wordWrap = true;
areaStyle.fontSize = 14;
areaStyle.margin = new RectOffset(2, 0, 10, 10);
areaStyle.CalcSize(new GUIContent(_textsWithWord[i].text));
EditorGUILayout.LabelField(_textsWithWord[i].text, areaStyle);
Thanks for your help in advance !
btw: Your CalcSize call is pretty pointless as you do not do anything with the returned Vector2.
Answer by Bunny83 · Jun 19, 2019 at 02:06 PM
How about using rich text tags? Just wrap the word in a color tag
"Some text before <color=yellow>borderline</color> and some text after"
If you want to do a search and highlight all the words you can simply use
highlightedText = originalText.Replace(searchWord, "<color=yellow>"+searchWord+"</color>");
Note that your "ignore case" option might give you some trouble. You would have to use either a RegEx to do the replacing or do the replacing "manually".
Thanks, it works perfectly ! :) Here is the code :
areaStyle.richText = true;
if (!String.IsNullOrEmpty(_wordToSearch)) EditorGUILayout.TextArea(_textsWithWord[i].text.Replace(_wordToSearch,"<color=yellow>" + _wordToSearch + "</color>"), areaStyle);
Yep I understand, I'll be carefull with the ignore case and find a solution !
Thank you :)
Your answer
Follow this Question
Related Questions
Changing color of select section of text 0 Answers
Color custom editor window ? 2 Answers
How to get a italic Labelfield (EditorGUIStyle) 0 Answers
Is color schemes possible? 0 Answers
Can a GameObject's full inspector be drawn in a custom Editor Window ? 1 Answer