- Home /
Textmesh Pro Input Field Rich Text
I have a Textmesh pro Input field, that is using rich text tags. However, when the user edits the input, the tag doesn´t get removed but stays there. Do you have any advice? Thanks in advance
Answer by Martin_Gonzalez · Mar 27, 2018 at 05:46 PM
I don't know if this is what you are looking for:
No, I have this text and when the user starts typing again, he doesn´t delete the word, but the tag. Do you know what I could do?
@$$anonymous$$GDevelopmentsSt Do you have Andy idea ?
I didn't work with Text$$anonymous$$esh Pro. But i cannot understand what you want to achieve. Try to break it in different steps.
I have an Input field which has markup tags in it. Those are not shown, because they directly format my text. However if the user then deletes the last character of the word, he doesn’t delete it, but deletes the tag. This means the word isn’t formatted anymore and the tags are shown. Do you know what I mean?
Answer by LilGames · Mar 27, 2018 at 08:05 PM
Don't use HTML mark-up in input fields (for the very reason you just experienced). If you want bold, assign a bold font to the field.
But then I can’t specify which words should be bold/italic/colored differently/underlined etc.
Answer by aciusa · Dec 07, 2018 at 05:22 PM
You can overload the TMP_InputField Class, its not perfect but here is a caret based solution to get you going in the right direction. This solution fails if the user types a tag and is either backspace or delete focused, depending on which of the ending if block logic statements is constrained by lastShiftRight.
EDIT: Updated so it works with both delete and backspace keys as normal, users can still mess it up by typing in their own tag. This probably needs another layer of detection to wrap with and ignore for the string search.
public class TagInputField : TMP_InputField {
private int lastCaretPos = 0;
private bool insideTag = false;
private bool infrontTag = false;
private bool behindTag = false;
private bool ranFrame = false;
private int lastOpen = 0;
private int lastClose = 0;
private int nextOpen = 0;
private int nextClose = 0;
private bool lastShiftRight = false;
private void OnGUI()
{
if (isFocused) //&& Input.anyKey
{
try
{
lastOpen = text.LastIndexOf('<', selectionStringAnchorPosition - 1);
lastClose = text.LastIndexOf('>', selectionStringAnchorPosition - 1);
nextOpen = text.IndexOf('<', selectionStringAnchorPosition);
nextClose = text.IndexOf('>', selectionStringAnchorPosition);
}
catch (System.ArgumentOutOfRangeException)
{
Debug.Log("end of text field");
}
if (lastOpen > lastClose || (nextClose < nextOpen && nextOpen != -1)) insideTag = true;
else insideTag = false;
if ((nextOpen - 1) == selectionStringAnchorPosition) behindTag = true;
else behindTag = false;
if ((lastClose + 1) == selectionStringAnchorPosition) infrontTag = true;
else infrontTag = false;
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
{
if (insideTag) caretPosition += 1;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
lastShiftRight = false;
}
else if (Input.GetKey(KeyCode.RightArrow))
{
lastShiftRight = true;
}
if (Input.GetKey(KeyCode.Delete))
{
if (insideTag)
{
DisableKey(KeyCode.Delete);
selectionStringAnchorPosition = nextClose + 1;
selectionStringFocusPosition = nextClose + 1;
}
lastShiftRight = true;
//DisableKey(KeyCode.Delete);
}
if (Input.GetKey(KeyCode.Backspace))
{
if (insideTag)
{
DisableKey(KeyCode.Backspace);
selectionStringAnchorPosition = lastOpen - 1;
selectionStringFocusPosition = lastOpen - 1;
}
if (infrontTag)
{
DisableKey(KeyCode.Backspace);
selectionStringAnchorPosition = lastOpen;
selectionStringFocusPosition = lastOpen;
}
lastShiftRight = false;
}
if (infrontTag && !lastShiftRight) // && !Input.GetKey(KeyCode.RightArrow)
{
selectionStringAnchorPosition = lastOpen;
selectionStringFocusPosition = lastOpen;
}
else if (insideTag && lastShiftRight)
{
selectionStringAnchorPosition = nextClose + 1;
selectionStringFocusPosition = nextClose + 1;
}
else { lastShiftRight = false; }
}
}
static void DisableKeys(KeyCode[] keys)
{
if (!Event.current.isKey)
{
return;
}
foreach (KeyCode key in keys)
{
if (Event.current.keyCode == key)
{
Event.current.Use();
}
}
}
static void DisableKey(KeyCode key)
{
DisableKeys(new KeyCode[] { key });
}
}
http://angryant.com/2009/09/07/i-bet-you-cant-type-an-a/ - credit for disable key script
Your answer
Follow this Question
Related Questions
How do I change "return"/"submit" button to "search" in native android keyboard in TMP_InputField? 1 Answer
Text MeshPro Input field insert integer? 1 Answer
Allow TMP_InputField to show HTML Tags even Rich Tags are ENABLED. 0 Answers
Text Mesh Pro : Unable to use pull down menu inspite of Distance field 16 0 Answers
Why does my TMP inputfield text disappears when I rotate It, why using a world space camera 0 Answers