Author Topic: Retrieving Highlighted Text from NGUI UI input  (Read 2883 times)

tsi25

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Retrieving Highlighted Text from NGUI UI input
« on: June 23, 2014, 10:53:48 AM »
I'm making an extremely basic text editor, and I would like the user to be able to enter text, and then change the color of a part of that text dynamically. I have a way to select a new color and to return a usable RGB value, and I have a UI Input that lets me put in multi-line text, and it even allows me to highlight sections of that text (highlighted text obviously behaves in all of the ways you would expect highlighted text to work.) What I'm having trouble figuring out is how to set the color of highlighted text. I know that I can set the color manually by typing in something like [FF0000]this text is colored[-] to the label but if I'm going to do that programmatically I still need to know where the beginning and the end of the highlighted text is.

So... what information can i retrieve about highlighted text, and how can I set the highlighted text (and the highlighted text only) to a new color?

I will continue to look through the NGUI script reference in the meantime and I'll let you all know if i find anything but if someone can point me in the right direction that would be fantastic!

tsi25

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Retrieving Highlighted Text from NGUI UI input
« Reply #1 on: June 23, 2014, 12:13:56 PM »
[UPDATE]

At this point what I'm doing is I'm hooking into a message being sent from the color picker and have its OnSetColor function which takes a color and then does effectively whatever I tell it to do with said color. At the moment what I'm telling it to do is to create a new string from the old one that puts in the color tags such as [FF0000] whatever [-] using the selectionStart and selectionEnd functions to determine the string indices where those tags should be inserted.

At this point my problem is that for some reason when I click on the UIInput and start typing, the BBCode parameter gets toggled to false which means that my color tags appear as text rather than being tags. Is there a way to prevent that from happening?


  1. public void Start()
  2.         {
  3.                 ColorPicker colorPicker = FindObjectOfType<ColorPicker>();
  4.                 colorPicker.receiver = gameObject;
  5.         }
  6.  
  7. public void OnSetColor(Color color)
  8.         {
  9.                 Debug.Log (color);
  10.                 string str = input.text;
  11.                 int start = input.selectionStart;
  12.                 int end = input.selectionEnd;
  13.  
  14.                 input.text = string.Format("{0}[{1}]{2}[-]{3}", str.Substring(0, start),color.ToHexRGB(),str.Substring(start, end - start), str.Substring(end));
  15.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Retrieving Highlighted Text from NGUI UI input
« Reply #2 on: June 24, 2014, 04:18:20 AM »
NGUI's input fields intentionally disable BBCode because supporting BBCode during input will make everything 10 times more complicated, especially when it comes to cursor positioning and highlighting.

tsi25

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Retrieving Highlighted Text from NGUI UI input
« Reply #3 on: June 24, 2014, 09:56:00 AM »
is there no way to re-enable it after the color tags have been inserted?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Retrieving Highlighted Text from NGUI UI input
« Reply #4 on: June 25, 2014, 05:21:20 AM »
There is no simple way to make this work with NGUI as NGUI's input fields were simply not designed to handle BBCode-enabled labels. You can comment out that line that turns off BBCode support in UIInput, but you will run into other issues.

tsi25

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Retrieving Highlighted Text from NGUI UI input
« Reply #5 on: June 25, 2014, 10:49:13 AM »
[UPDATE]

I gonna have to side with ArenMook on this one theres really no easy way to make it work. What I'm trying to implement at this point (and I'm having some success with it) is using both an input field and a label to sort of mimic the desired effect. What that means is I'm drawing the input field's label behind the label such that both labels perfectly overlap and when the user goes to select or highlight, it looks as though they are interacting with the label. I can then use the indices of where they've highlighted in the input field's label to place the color tags in the label's text.

The label will process the bbcode tags and compress to the same size as the input field's label (as in the characters in the newly colored label will cover up the uncolored characters in the input field's label.) Very convoluted, and I don't have it working completely yet

I'll put code up and a more detailed walkthrough of dynamically editable and colorable text if I have any success making my code less brittle.