In XNA I am building my own textbox, and listbox.

For the textbox, I want it to render all text it can, and then cut off at the end of the textbox. But I don't want it to cut whole letters. I want it to cut exactly where the pixel limit is, so that if I write "SPAAAAAAAACE", and the size is 50 for instance, then it will only render "SPAAAA" and then a half A, or whatever is remaining in the pixels.

For my listbox, it's kind of the same thing. If I am scrolled half down, one element needs to be only half-visible.

How can I do this? Is it possible to add something to a buffer, and then before drawing it, clearing a specific rectangle from the buffer again?


2 Answers 2


You could use a scissor rectangle. It's a property on the GraphicsDevice class that you can set. Scissor testing must also be enabled.

The scissor rectangle only allows draws in a certain area (a rectangle). This would cut off the pixels, not the letters, of the text you are trying to draw. There are other ways to do this, but you might find this to be the easiest.


Set your GraphicsDevice.Viewport or GraphicsDevice.ScissorRectangle so that you are only drawing over the top of your text area. You must be outside of a sprite-batch when you do this (ie: between end and begin). Remember to set it back when you're done.

Remember that SpriteBatch's drawing area covers the viewport (so (0,0) is the top-left of the viewport).

  • \$\begingroup\$ Wow, literally didn't see this till after I posted... \$\endgroup\$ Commented May 29, 2011 at 2:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .