How to change the font while playing XNA?

I need to use different spritefont fonts, do I need to create a new spritefont for each size?

+6
source share
3 answers

I guess, yes.

SpriteBatch.DrawString has an overload that gives you the ability to scale text.

However, the main drawback of this is that your text will become jagged when you scale it. If you start at a higher resolution and zoom out, you will begin to receive artifacts when you can get smaller sizes.

So, if you have a fixed number of sizes, you should create several versions of the sprite font with different sizes that you need.

If you want constantly scalable text with sharp edges, you can perhaps explore vector fonts. The Nuclex Framework has code for this .

+22
source

You can also make your font the largest size you need and reduce it from there.

+3
source

Suppose the SpriteFont you are using is called x.spritefont. Follow these steps to create a new SpriteFont for each size.

  • Open the x.spritefont file from Solution Explorer.
  • Scroll to the tag and edit it to the desired size.
  • To make a large font, duplicate the file and change accordingly. Rename files with added size, finally, for easy memorization.

Now create some instances of SpriteFont and load them accordingly.

 SpriteFont sf_s10; SpriteFont sf_s14; protected override void LoadContent() { sf_s10 = Content.Load<SpriteFont>("x_10"); sf_s14 = Content.Load<SpriteFont>("x_14"); //OTHER LOADS } 

To dynamically change fontSize follow these steps:

 SpriteFont current_font; protected override void Update(GameTime gameTime) { if(/*SOME_CONDITION_TO_DECREASE_SIZE*/) current_font=sf_s10; if(/*SOME_CONDITION_TO_INCREASE_SIZE*/) current_font=sf_s14; } 
+3
source

Source: https://habr.com/ru/post/886464/


All Articles