Today I was reading up on how to load TrueType fonts into MonoGame (version 3.7.1), but couldn’t get the TTF file to build with the MonoGame Pipeline Tool. In this tutorial, I will be covering my slight workaround in order to load TTF fonts into MonoGame.
First, create a new MonoGame project if you haven’t already. I chose the “MonoGame Cross Platform Desktop Project” template. The next thing I did was open the Content.mgcb file in the Content folder of my project with the MonoGame Pipeline Tool.
Click the “Add Existing” button on the top toolbar, then opened the TTF file I wanted to use. Once I clicked “Open,” another prompt appeared asking if I wanted to copy the file or link it. Make sure you choose copy the file, the click “OK”. Select the TTF file under the project pane, and change the build action from Build to Copy in the Properties pane. This is what allowed me to work around this build issues I was having. After that, you will want to right click the Content project under the project list (found in the pane on the left of the window). On the drop-down menu, go to Add > New Item… and click on that option. A new prompt will appear where you can type the name you want the SpriteFont to have. It does not need to be the same name as the TTF file. Ensure that “SpriteFont Description (.spritefont)” is chosen. Once you’re ready, click the “Create” button. The spritefont file should now be added to our project’s content pipeline. Now, we will have to right click our newly created spritefont file and choose “Open With”. Select a text or code editor of your choice and open the file. Between the <FontName> tags in the spritefont file, add the file name of the TTF file you added including the .ttf extension. Feel free to adjust any other properties you need while you’re still in the spritefont file. Once you’re done, close out of your editor and head back to Visual Studio. At the top of your main game file, Game1.cs, under the declaration of the SpriteBatch, add the following line:
SpriteFont myFont; // Feel free to name "myFont" to anything else.
Once we’ve declared the SpriteFont, we will need to add it to our LoadContent method. After the comment, “TODO: use this.Content to load your game content here,” add the following line:
myFont = Content.Load<SpriteFont>("myFont");
Now, let’s try drawing a string with our font! In the Draw method, add the following code:
spriteBatch.Begin(); spriteBatch.DrawString(myFont, "Hello world!", new Vector2(32, 32), Color.White); spriteBatch.End();
Note, if you have already added calls to spriteBatch.Begin and spriteBatch.End, you don’t need to add them from the code above. When we go to run our game, we should now see something like the following:
It works! Big thanks to Reekee of Dimenzioned on dafont.com for posting the awesome free font, Arcadepix! Even though I followed the MonoGame documentation for using TrueType fonts (which you can find here), the article doesn’t mention anything about setting the build action to Copy for the TTF file. Perhaps I’m missing something? I’m not sure. If anyone out there has a better/official solution, I would love to hear it! In the meantime, hopefully this guide will be able to help some of you out.
This post was written by Jared York