使用 XNA 设置 C# 中文本的格式

发布于 2024-09-01 15:03:22 字数 350 浏览 8 评论 0原文

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

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

因此 标记将表示红色文本, 标记将表示蓝色文本。
我想确切地知道红色文本从哪里开始以及蓝色文本从哪里开始,以便我可以单独渲染它们。
您有什么建议可以做什么,以及使用什么来做到这一点?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-09-08 15:03:22

我建议使用两种方法来执行此操作

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

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

然后您需要一个如下所示的绘制方法:

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

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
    }
}
扭转时空 2024-09-08 15:03:22

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文