- Home /
Color specific words in Input Field
I want my User to be able to write in the input field and specific words to be coloured differently - like Syntax Highlighting. However, if I do this in the OnValueChanged Method, this method gets executed again, because I add the attribute. This leads to a StackOverflow Exception.
Do you have any suggestions?
Thank you in advance!
Answer by Martin_Gonzalez · Mar 26, 2018 at 05:14 PM
Yes you can!
Have you tried this?
https://docs.unity3d.com/Manual/StyledText.html
Example
We are <color=#00ffffff>not</color> <b>amused</b
Yeah,
`int charIndex = myText.IndexOf ( "S" );
string newText = myText.Replace ( myText [charIndex].ToString (), "" + myText [charIndex].ToString () + "" );`
but then the text gets changed again and the OnValueChange $$anonymous$$ethod gets executed which means that my code will insert another attribute which causes the error.
(my color attributes are not shown correctly, they are in the empty string)
Yes, you will need in every OnValueChange replace again the text.
Perhaps you can create a method that you send the current text split it and check the words. You can have a dictionary with the word and the styled text you want.
Create a UI Text in the scene and add this script on it. Tell me if you understand it.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
public Text myText;
Dictionary<string, string> myStyledWords = new Dictionary<string, string> {
{"word", "<b>word</b>"}
};
private string userInput = "This has a bold word";
private void Start() {
myText = GetComponent<Text>();
var text = StyledText(userInput);
myText.text = text;
}
public string StyledText(string userInput) {
var splitted = userInput.Split(' ');
return splitted.Aggregate((acc, word) => {
var finalWord = word;
if (myStyledWords.Contains$$anonymous$$ey(word))
finalWord = myStyledWords[word];
return acc + ' ' + finalWord;
});
}
}
Apart from the aggregate, I understand it, but does it work when I change the text during runtime?
I recently tested it but it will not work on InputFields (On Text yes will work) but Input Field do not support Rich Text :(
The Text property of the Text control itself will change as the user types and the value can be retrieved from a script after editing. Note that Rich Text is intentionally not supported for editable Text controls; the field will apply any Rich Text markup instantly when typed but the markup essentially “disappears” and there is no subsequent way to change or remove the styling.
I have an idea. What if you add an extra Text component to the input field. This text component will display your stylized text. The input field will write to its default text component that will be invisible (just set the color alpha to 0 or something) then with OnValueChanged you do whatever check you need to do but you don't change the actual value you just pass it along to the extra Text component that will display it, that way it, OnValueChanged won't be triggered again and you can stylize the text however you want.
Your answer
Follow this Question
Related Questions
Changing just the name of person in UI Text 0 Answers
Customize Inputfield Unity Plus/Pro 0 Answers
Hide Textselection in Scrollable Inputfield 1 Answer
How do I change "return"/"submit" button to "search" in native android keyboard in TMP_InputField? 1 Answer
Prevent focus from leaving inputfield 0 Answers