开发者

How to check if a font supports a specific style

开发者 https://www.devze.com 2023-04-01 06:30 出处:网络
I\'m getting the following exception when changing my application font, because I use a strike out in a part of my application, and some fonts don\'t support it:

I'm getting the following exception when changing my application font, because I use a strike out in a part of my application, and some fonts don't support it:

How to check if a font supports a specific style

I change my application font using a font dialog. I need to check if the selected font supports the strikeout style after assigning it to my application.

What is the recommended way to do this? I know I could create a font with the style and catch the exception, but is there a more elegant way to do it?

Thanks in advance.


EDIT: The user selects a font, not necesary strikeout. In that moment I need to check if the font supports the style strikeout, because I create a strikeout font in a part of my application. If the font don't support the strikeout style w开发者_Go百科ould not allow the user to choose that font.


Updated : (to reflect update in the initial post):

InstalledFontCollection ifc = new InstalledFontCollection();
for (int i = 0; i < ifc.Families.Length; i++)
    {
         if (ifc.Families[i].IsStyleAvailable(FontStyle.StrikeOut))
         {
             //add particular font with this family to your "font selector"
         }
    }


If you are using the standard Font class, then you can use the Font.Strikeout property:

//Gets a value that indicates whether this Font specifies a horizontal line through the font.
public bool Strikeout { get; }


Finally I used the following:

    private bool SupportStrikeout(Font font)
    {
        try
        {
            using (Font strikeout = new Font(font, FontStyle.Strikeout))
            {
                return true;
            }
        }
        catch (ArgumentException)
        {
            return false;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消