views:

102

answers:

2

Hello, I'm currently trying to make a TextBox for my GUI with XNA, and I was wondering how could I find tagged text in a string.
For instanceI have this kind of text:

Hey there, I was <r>going to</r> the <b>Mall</b> today!

So the <r> tag would represent red text and the <b> tag would represent blue text.
And I want to know exactly where the red text starts and where the blue text starts so I could render them separately.
Do you have any suggestion what to do about it, and what to use for doing that?

Thanks in advance.

A: 

Well you could just parse the line and when you reach a set a color property of your text so that it will now render blue but it will have to be a separate render call or else the whole string will turn blue. So if you make a new string when you come upon a tag then set the color property then render that string then that should work.

Chris Watts
This isn't a very flexible way to do things, nor is it explained with any amount of clarity.
mikeschuld
Yeah my bad I was in a hurry =/
Chris Watts
+1  A: 

I would suggest doing this with two methods

First, have a method that can take your string and return a collection of string color pairs:

struct StringColorPair {
    public string myText;     // the text
    public Color myColor;     // the color of this text
    public int myOffset;      // characters before this part of the string 
                              // (for positioning in the Draw)
}

public List<StringColorPair> ParseColoredText(string text) {
    var list = new List<StringColorPair>();    

    // Use a regex or other string parsing method to pull out the
    // text chunks and their colors and then for each set of those do:
    list.Add(
        new StringColorPair {
            myText = yourParsedSubText,
            myColor = yourParsedColor,
            myOffset = yourParsedOffset }
    );

    return list;
}

Then you would need a draw method like this:

public void Draw(List<StringColorPair> pairs) {
    foreach(var pair in pairs) {
        // Draw the relevant string and color at its needed offset
    }
}
mikeschuld