質問

Im working in xna and my problem is the following. I got a text where the first occurence of " ________ " should be highlightet for the user in some way. This could be done by making the fontsize of this part bigger, highlighting or some other way if anyone got a great idea.

public void DrawStringWithStyle(SpriteBatch batch, SpriteFont thisFont, Vector2 pos, string thisText, SpriteFont BoldFont)
    {
        string[] paragraphs = Regex.Split(thisText, @"\\(c[a-fA-F0-9]{6})|\\(b)|\\(o)|\\(l)");
        SpriteFont CurrentFont = font;
        float tempPosX = pos.X;

        for (int i = 0; i < paragraphs.Length; i++)
        {
            batch.DrawString(CurrentFont, paragraphs[i], new Vector2(tempPosX, pos.Y), Color.Black);

            if (i + 1 < paragraphs.Length)
            {
                tempPosX += CurrentFont.MeasureString(paragraphs[i]).X;

                i++;

                switch (char.ToLower(paragraphs[i][0]))
                {
                    case 'o': CurrentFont = font; break;
                    case 'b': CurrentFont = BoldFont; break;
                    case 'l':
                        paragraphs[i+1] = paragraphs[i+1].Insert(0, Environment.NewLine);
                        tempPosX = pos.X;
                        break;
                }
            }
        }
    }

So i got 2 new problems as you might be able to tell. One of them is that 2 commands musnt come in a row because then it will screw up bigtime, need to be able to check if the next is a command or if its a normal paragraph somehow. The other problem needs a solution quite like that because my (l) command only works if the following paragraph isnt a command. Any idea on how to fix my 2 problems?

役に立ちましたか?

解決

Split the text that has different styles... and draw each part with its style.

You can use a \c to change color: "My \cFF5566favaourite \cFFFFFFgame is \c444444warcraft 3", or \b to use bold font...

public static void DrawStringWithStyle( this SpriteBatch batch, SpriteFont font, Vector2 pos, string text, Color color, SpriteFont BoldFont=null )
{
    string[] paragraphs = Regex.Split( text, @"\\(c[a-fA-F0-9]{6})|\\(b)|\\(n)" );
    Color CurrentColor = color;
    SpriteFont CurrentFont = font;

    for ( int i=0; i< paragraphs.Length; i++ )
    {
        batch.DrawString( CurrentFont, paragraphs[i], pos, CurrentColor );

        if ( i+1<paragraphs.Length )
        {
            pos.X += CurrentFont.MeasureString( paragraphs[i] ).X;
            i++;

            switch (char.ToLower(paragraphs[i][0]))
            {
                case 'c':
                    CurrentColor.R = byte.Parse( paragraphs[i].Substring( 1, 2 ) );
                    CurrentColor.G = byte.Parse( paragraphs[i].Substring( 3, 2 ) );
                    CurrentColor.B = byte.Parse( paragraphs[i].Substring( 5, 2 ) );
                    break;
                case 'n': CurrentFont = font; break;
                case 'b': CurrentFont = BoldFont; break;
            }
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top