Replacing a selected part of string in a GUI.Textfield
Hey there folks,
my problem is quite "simple" (famous last words of every coder), yet I struggle to find any useful documentation/examples in the interwebs.
So basically I have this text editor, and it's supposed to replace a selected part of a string in a TextField/Area into another string, example:
it's supposed to turn "Hello my name is Ed" into "Hello my name is <ED>".
Now I can already get which part of the entered string the user has selected with the TextEditor:
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
string selectedText = editor.SelectedText;
So the part of the text is selected and now need to be replaced, and here's where I have no clue to go about it, because the TextEditor, does have a "ReplaceSelection" method, but it doesn't seem to work the way I want it (it just changes whats exactly selected). I would appreciate any help/hints towards solving this matter! :)
P.S: Just in case it's not clear, I don't want to replace the finished string, that would be fairly easy, but the string that is still being written/editted by the user in the Textfield/Area (imagine a WYSIWYG editor-like behavior).
Answer by Scribe · Sep 09, 2015 at 01:33 PM
public string txt = "";
void OnGUI(){
txt = GUI.TextArea(new UnityEngine.Rect(8, 8, 200, 200), txt);
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
GUI.Label(new UnityEngine.Rect(216, 8, 200, 200), string.Format("Selected text: {0}\nPos: {1}\nSelect pos: {2}",
editor.SelectedText,
editor.pos,
editor.selectPos));
if (GUI.Button(new UnityEngine.Rect(8, 216, 400, 20), "Replace")){
txt = txt.Remove(Mathf.Min(editor.pos, editor.selectPos), editor.SelectedText.Length);
string replaceString = "hahaha";
txt = txt.Insert(Mathf.Min(editor.pos, editor.selectPos), replaceString);
editor.pos = Mathf.Min(editor.pos, editor.selectPos);
editor.selectPos = editor.pos + replaceString.Length;
}
}
Ha! Not even saying a word, are we? Doesn't matter, it does precisely what I wanted, thanks a bunch! :)
Apologies, my post was a little rushed, Selected text is useful to know what you are replacing, though as there could be several places where a piece of text is present, it might not be correct to simply find the first instance of the text.
editor.pos is where the cursor was first clicked, editor.selectedPos was where the cursor was dragged to, hence the $$anonymous$$imum of the two is the first position of the selected text and the $$anonymous$$imum plus the length of the selected text is the end! you could also use the maximum of the two to find the end!
Remove with two integer arguments x and y, removes the text from the string between the positions x and y! Insert... well.. it inserts stuff at the position specified!
It is not very visible due to the new site layout, but I have linked to another question at the bottom of my post, for where I got the information from!
Your answer
Follow this Question
Related Questions
Specifying custom Bounds for a component, for use with the '(F)rame' shortcut 0 Answers
Input not updating on user input 1 Answer
Plane character selector unity problem 0 Answers
I need help finding the index of an object in a list. 1 Answer
If instantiate prefab is selected how can i change the color? 1 Answer