1

我目前正在尝试使用 XNA 为我的 GUI 制作一个 TextBox,我想知道如何在字符串中找到标记的文本。
例如我有这样的文字:

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

所以<r>标签代表红色文本,<b>标签代表蓝色文本。
我想确切地知道红色文本从哪里开始,蓝色文本从哪里开始,这样我就可以分别渲染它们。
你有什么建议该怎么做,用什么来做这件事?

提前致谢。

4

2 回答 2

1

我建议用两种方法做到这一点

首先,有一个方法可以获取您的字符串并返回字符串颜色对的集合:

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

然后你需要一个这样的draw方法:

public void Draw(List<StringColorPair> pairs) {
    foreach(var pair in pairs) {
        // Draw the relevant string and color at its needed offset
    }
}
于 2010-07-01T20:11:25.157 回答
-1

好吧,您可以只解析该行,当您到达文本的一个颜色属性时,它现在将呈现蓝色,但它必须是一个单独的渲染调用,否则整个字符串将变为蓝色。因此,如果您在遇到标签时创建一个新字符串,然后设置颜色属性,然后渲染该字符串,那么它应该可以工作。

于 2010-06-20T17:33:53.037 回答