Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 使用XNA在C#中格式化文本_User Interface_C# 4.0_Text Formatting_Colors_Xna 4.0 - Fatal编程技术网

User interface 使用XNA在C#中格式化文本

User interface 使用XNA在C#中格式化文本,user-interface,c#-4.0,text-formatting,colors,xna-4.0,User Interface,C# 4.0,Text Formatting,Colors,Xna 4.0,我目前正在尝试使用XNA为我的GUI创建一个文本框,我想知道如何在字符串中找到标记的文本。 例如,我有这样的文本: Hey there, I was <r>going to</r> the <b>Mall</b> today! 嘿,我今天要去购物中心! 因此,标记将代表红色文本,标记将代表蓝色文本。 我想知道红色文本从哪里开始,蓝色文本从哪里开始,这样我可以分别渲染它们。 你有什么建议要怎么做,用什么做 提前感谢。您可以解析该行,当您达到设置文

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

Hey there, I was <r>going to</r> the <b>Mall</b> today!
嘿,我今天要去购物中心! 因此,
标记将代表红色文本,
标记将代表蓝色文本。
我想知道红色文本从哪里开始,蓝色文本从哪里开始,这样我可以分别渲染它们。
你有什么建议要怎么做,用什么做


提前感谢。

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

我建议用两种方法来做这件事

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

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;
}
struct-StringColorPair{
公共字符串myText;//文本
公共颜色myColor;//此文本的颜色
public int myOffset;//字符串此部分之前的字符
//(用于在图纸中定位)
}
公共列表ParseColoredText(字符串文本){
var list=新列表();
//使用正则表达式或其他字符串解析方法拉出
//文本块及其颜色,然后对每组文本块执行以下操作:
列表。添加(
新色对{
myText=yourParsedSubText,
myColor=yourParsedColor,
myOffset=yourParsedOffset}
);
退货清单;
}
然后您需要这样的绘制方法:

public void Draw(List<StringColorPair> pairs) {
    foreach(var pair in pairs) {
        // Draw the relevant string and color at its needed offset
    }
}
public void Draw(列表对){
foreach(成对变量对){
//在所需偏移量处绘制相关字符串和颜色
}
}

这不是一种非常灵活的做事方式,也没有任何清晰的解释。是的,我的错,我当时很匆忙=/