Search Unity

TextMesh Pro Looking for some assistance on getting selected text in tmpro inputfield.

Discussion in 'UGUI & TextMesh Pro' started by Wtyson, May 26, 2022.

  1. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    57
    I am currently working on a text editor for a project I am working on and I am having issues getting the selected text. Like let's say I have a sentence: "Tom is not a man but a dog" And I highlight 'not a man' And want to surround it with bold tags with a button press like a normal text editor. I can get it to work a little bit using selectionAnchorPosition and selectionFocusPosition with the following code.


    Code (CSharp):
    1.  
    2.  
    3.     public void getSelected() {
    4.         startPos = bodytext.selectionAnchorPosition;
    5.         endpos = bodytext.selectionFocusPosition;
    6.  
    7.  
    8.     }
    9.     public void maketextbold() {
    10.  
    11.  
    12.         bodytext.text = bodytext.text.Insert(startPos, "<b>");
    13.         bodytext.text = bodytext.text.Insert(endpos, "</b>");
    14.  
    15.         Debug.Log("start: " + startPos);
    16.         Debug.Log("end: " + endpos);
    17.     }
    it semi-works but is very inconsistent and buggy. The void getSelected() is a function called on the Onselect event on the tmp_pro input field. while the maketextbold() function is a button press event. Sometimes it works sometimes it causes the bold tags to go all over or it will just ignore the highlight altogether and return 0 for start and end. And there are times when it gives an error and says the length is out of range when I am highlighting the middle of a word. If anyone has any ideas on how to get this to work properly and can help me out that would be very much appreciated.

    Thanks in advance....
     
    Last edited: May 27, 2022
  2. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    57
    Still looking for assistance on this. It seems text mesh pro is not able to get the text properly when highlighting text.
     
  3. IvanT77

    IvanT77

    Joined:
    Oct 16, 2020
    Posts:
    1
    I did it like this.
    Code (CSharp):
    1.  int startCaret = selectionAnchorPosition;
    2.         int endCaret = selectionFocusPosition;
    3.  
    4.         if (selectionAnchorPosition > selectionFocusPosition)
    5.         {
    6.             startCaret = selectionFocusPosition;
    7.             endCaret = selectionAnchorPosition;
    8.         }
    9.  
    10. var startPos = textComponent.textInfo.characterInfo[startCaret].index;
    11. var endPos = textComponent.textInfo.characterInfo[endCaret].index;
     
  4. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    57
    I will give it a try and see if I can get it to work thanks.
     
  5. Wtyson

    Wtyson

    Joined:
    Aug 15, 2014
    Posts:
    57
    I tried this but it still does not work. I always get 0 returned for both start and end. I never thought it would be so hard to highlight some text at runtime and hit a button and make the text that is highlighted go bold.
     
  6. shieldgenerator7

    shieldgenerator7

    Joined:
    Dec 20, 2015
    Posts:
    39
    The problem is that the input field lost focus event gets processed before the button click event, and the input field clears its selection when it loses focus. So when you're storing your own selection variables, you have to specify to only do it when the input field has focus.

    Code (CSharp):
    1.  
    2.     public TMP_InputField txtMessage;
    3.     public int SelectionStart { get; private set; }
    4.     public int SelectionEnd { get; private set; }
    5.  
    6.     void Start()
    7.     {
    8.         txtMessage.onTextSelection.AddListener(selectText);
    9.     }
    10.  
    11.     public void selectText(string str, int pos1, int pos2)
    12.     {
    13.         if (txtMessage.isFocused)
    14.         {
    15.             SelectionStart = Mathf.Min(pos1, pos2);
    16.             SelectionEnd = Mathf.Max(pos1 - 1, pos2 - 1);//minus one to convert from caret pos to character pos
    17.         }
    18.     }
    19.  
    Then you can use your own internal selection variables in your code to manipulate the selection.

    NOTE: This does not keep the input field from clearing the selection. You'll probably have to then use your internal variables to restore the input field's previous selection once it regains focus.
     
    mowax74 and CognitiveCode like this.