Unicode characters

I need to display font characters for languages other than English. For example a typical example is a umlaut used for German, Swiss , et. all. Example : ä

How can I do this? Currently I am using the Spritefont class but get an error when I try to display this or other characters such as ä ë ï ö ü.

Update or add <CharacterRegion> value(s) in your *.spritefont content file(s). These values define the character ranges that get built into the SpriteFont.

1 Like

Thanks, If you or others have any articles or info you can point me too, that would be great. I am building a mobile popup keyboard in C# for XNA/Monogame, and would eventually like to share the end product with other developers once it is completed. The Keyboard is complete however I only need to add in the code to account for unicode characters for latin and non asian languages. For example : ä, ö, ü,
á,Á,é,É

(No offense to the asian countries, I just don’t have the time to account for non latin characters right now. Maybe in subsequent releases.)

I’m not sure what your use case is, but it would almost certainly be better to just let the user use the keyboard they’re familiar with (i.e. open system keyboard and handle key press events).

That said, all you need to do is set your <CharacterRegion> values within your .spritefont content file. I generally write a tool that runs through any text that I will display onscreen and have it generate my .spritefont files for me, looking through characters used and only adding ranges for those that actually appear, but you can easily fix your error by just adding the basic Latin ranges you need. See this table for a list of common ranges, as well as specifically what each index represents: Unicode Chart

So, if you wanted your SpriteFont to be able to render pretty well any Latin-based character, you would just add the Latin character subset ranges. I.e. 0-127 (though you generally only actually need 32-127 here, since the first ones are for stuff like line feeds and various other not-commonly-used-anymore whitespace or meta-data stuff), 128-255, 256-383, and 384-591. So, you could accomplish all that with a simple definition of a single range encompassing all of it, 32-591, though doing so may make your generated SpriteFont quite large as it’ll include a lot of not used characters (decimal indexes are defined with &#val;):

<CharacterRegions>
	<CharacterRegion>
		<Start>&#32;</Start>
		<End>&#591;</End>
	</CharacterRegion>
</CharacterRegions>

Alternatively you can use https://github.com/rds1983/FontStashSharp or https://github.com/rds1983/SpriteFontPlus. To get the characters you need on demand.

1 Like

Thanks TheKelsam, just curious if for Android and Apple if you have ever yourself (Or know of someone, or know of an XNA game) who has used the open system keyboard for a Game written in XNA/Monogame? Is it fairly easy to use the existing open system keyboard without causing issues for the Game? I have seen a few post online where people end up writing a popup to prompt for a persons name instead of using the open system keyboard which you are talking about. I could see it working fairly easily for the Android platform, but for Apple IOS I am not sure?

I have. I have done both “actual” integration (manually opening/closing the system keyboard and handling keypress events like any hardware keyboard), as well as MG’s simple “get some text from the user” method. If all you need is a way for the user to provide a short string, I recommend checking out KeyboardInput.Show which pops up a simple dialog box with title/description/textbox for the user to provide simple text input on mobile. You can see this being used in one of our games, Party Words, for iOS and Android (used in the “Join a Game” process, when the user inputs a room code).

Not directly, but somewhat related, MG also provides a simple method for displaying user alerts on mobile: MessageBox

Great, thanks TheKelsam