Search code examples
c#androidiosxnamonogame

How can I find out if a string contains characters that cannot be resolved by this SpriteFont?


I get an exception when I draw characters that are not supported in my SpriteFont. TextboxString is a string that was entered with the soft keyboard. The player can enter his display name, password and e-mail adress with the soft keyboard in my iOS/Android application.

    private async Task<string> ShowKeyboard(string messageboxtitle, string messageboxdescription, string text, bool ispassword)
    {
        string TextboxString = "";
        await Task.Run(async () =>
        {           
            var result = await KeyboardInput.Show(messageboxtitle, messageboxdescription, text, ispassword);
            if (null != result)
            {
                TextboxString = result;
            }
        });

        KeyboardActiv = false;

        return TextboxString;
    }

For example, if TextboxString = "Player_磨"; then I get an exception because I don't support chinese characters. I only want to support English, Spanish, Italian, French, German and Portuguese characters.

spriteBatch.DrawString(Font, TextboxString, new Vector2(100, 500), Microsoft.Xna.Framework.Color.Black);

System.ArgumentException has been thrown

Text contains characters that cannot be resolved by this SpriteFont. Parameter name: text

How can I find out if a player entered a character that is not supported in my spriteFont?

I want to display a message in my application if the player entered a character that is not supported.

My spriteFont file:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Arial</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>28</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Regular</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>?</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
      <Start>&#32;</Start>
      <End>&#512;</End>
      </CharacterRegion>
      <CharacterRegion>
      <!-- Apostrophe 8217 -->
      <Start>&#8216;</Start>
      <End>&#8222;</End>
      </CharacterRegion>
      <CharacterRegion>
      <!-- Currency symbols -->
      <Start>&#8352;</Start>
      <End>&#8378;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>

Solution

  • You have two options here:

    1) Use SpriteFont.Characters.Contains to check if entered characters are represented in the given spriteFont file.

    2) Use regular expressions. Proper RegEx examples for eMail-validation and usage of (e.g.) certain characters and numbers are easily to find.

    EDIT: some kind of eMail check is recommended anyway to make sure users stick to the 'local-part@domain-part'