xnadrawstring

XNA SpriteFont with multiple colors?


Is there a way to draw a text with multiple colored horizontal areas? As an example I want to draw the text "Hello" with the lower half in red and the upper in green. The text to use has to be dynamic. With a normal Texture I would just use the Draw method and pass a subrectangle to draw parts of the texture in different color but for text?


Solution

  • The best method would probably involve custom shaders. But, you should be able to do it with scissor rectangles too.

    1. Draw the text normally in your upper color (green)
    2. Set up a ScissorRectangle that allows drawing only on the bottom half of the text
    3. Draw the text normally, using your bottom color (red). The top half should be clipped so the green text should remain there.

    To clarify, your text should be drawn in the same place both times.