开发者

Formatting Text in C# with XNA

开发者 https://www.devze.com 2022-12-30 22:24 出处:网络
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.

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 wou开发者_JAVA百科ld 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.


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
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消